/html

HTML template helper for Go

Primary LanguageGo

HTML

This package is mainly copied from gofiber framework, added some minor modifications for my own use cases.

HTML is the official Go template engine html/template, to see the original syntax documentation please click here or read this cheatsheet

Basic Example

./views/index.html

{{template "partials/header" .}}

<h1>{{.Title}}</h1>

{{template "partials/footer" .}}

./views/partials/header.html

<h2>Header</h2>

./views/partials/footer.html

<h2>Footer</h2>

./views/layouts/main.html

<!DOCTYPE html>
<html>

<head>
  <title>Main</title>
</head>

<body>
  {{embed}}
</body>

</html>
package main

import (
	"log"
	"net/http"

	"github.com/nguyendangminh/html"
)

func main() {
	// Create a new engine
	engine := html.New("./views", ".html")
	if err := html.SetDefaultEngine(engine); err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/layout", func(w http.ResponseWriter, r *http.Request) {
		html.Render(w, "index", map[string]string{"Title": "Hello, world!"}, "layouts/main")
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		html.Render(w, "index", map[string]string{"Title": "Hello, world!"})
	})

	log.Println("listen and serve at http://localhost:1203")
	log.Fatal(http.ListenAndServe(":1203", nil))
}

Example with embed.FS

Note:

  • Need Go 1.16 or later
  • Template names must be prefixed with template directory views
html.Render(w, "views/index", map[string]string{"Title": "Hello, world!"}, "views/layouts/main")
{{template "views/partials/footer" .}}
package main

import (
	"embed"
	"log"
	"net/http"

	"github.com/nguyendangminh/html"
)

//go:embed views/*
var viewsfs embed.FS

func main() {
	// Create a new engine
	engine := html.NewFileSystem(http.FS(viewsfs), ".html")
	if err := html.SetDefaultEngine(engine); err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/layout", func(w http.ResponseWriter, r *http.Request) {
		html.Render(w, "views/index", map[string]string{"Title": "Hello, world!"}, "layouts/main")
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		html.Render(w, "views/index", map[string]string{"Title": "Hello, world!"})
	})

	log.Println("listen and serve at http://localhost:1203")
	log.Fatal(http.ListenAndServe(":1203", nil))
}