Categorygithub.com/sfomuseum/go-template
modulepackage
1.10.0
Repository: https://github.com/sfomuseum/go-template.git
Documentation: pkg.go.dev

# README

go-template

Opinionated Go package for loading templates with default functions.

Documentation

Go Reference

Example

package main

import (
	"bufio"
	"bytes"
	"context"
	"embed"
	"github.com/sfomuseum/go-template/html"
	"strings"
)

//go:embed html_test.html
var FS embed.FS

func main() {

	ctx := context.Background()

	tpl, _ := html.LoadTemplates(ctx, FS)

	test_t := tpl.Lookup("test")

	type TestVars struct {
		PageTitle string
	}

	vars := TestVars{
		PageTitle: "This is a test",
	}

	var buf bytes.Buffer
	wr := bufio.NewWriter(&buf)

	test_t.Execute(wr, vars)
}

Where html_test.html looks like this:

{{ define "test"}}
<html>
	<head>
		<title>{{ if (IsAvailable "PageTitle" .) }}{{ .PageTitle }}{{ end }}</title>
	</head>
	<body>{{ if (IsAvailable "UserName" .) }}Hello {{ .UserName }}{{ else }}This is a test.{{ end }}
	</body>
</html>
{{ end }}

# Packages

Package css provides methods for loading CSS (.css) templates with default functions.
Package funcs provides default functions to include with templates.
Package html provides methods for loading HTML (.html) templates with default functions.
Package text provides methods for loading text (.txt) templates with default functions.