template

package module
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: BSD-3-Clause Imports: 0 Imported by: 0

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 }}

Documentation

Overview

Package template provides opinionated methods for loading templates with default functions.

Directories

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL