wolfeidau / echo-go-templates

This project provides some template helpers with the Go IOFS and HTML templates packages for use with echo.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

echo-go-templates

This project provides some template helpers with the Go IOFS and HTML templates packages for use with echo.

Usage

The following code example sets up views with a common layout, and includes for a header and footer. The names of files in the pages directory are used to render the templates.

	e := echo.New()

	render := templates.New()

	err := render.AddWithLayoutAndIncludes(views.Content, "layout.html", "includes/*.html", "pages/*.html")
	if err != nil {
		log.Fatal().Err(err).Msg("failed to load layout")
	}

	e.Renderer = render

This expects a directory structure such as:

views
  layout.html
  includes
    footer.html
    header.html
  views.go
  pages
    index.html

Note take a look at the test view templates in test/views

In this structure views.go uses the embed feature in Go to include all the templates in the Go binary.

package views

import "embed"

//go:embed pages/* includes/* layout.html
var Content embed.FS

To render the index page in this hierarchy.

    return c.Render(http.StatusOK, "index.html", nil)

Links

License

This application is released under Apache 2.0 license and is copyright Mark Wolfe.

About

This project provides some template helpers with the Go IOFS and HTML templates packages for use with echo.

License:Apache License 2.0


Languages

Language:Go 87.3%Language:Makefile 9.4%Language:HTML 3.3%