templating

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package templating provides an `Engine` for managing templates stored in an embedded filesystem. Use `Parse` to load multiple templates (via glob patterns), and `Render` to execute them with custom data.

Templates are HTML: data is escaped for the context it lands in, so a value carrying markup is rendered as text. Pass template.HTML to opt a value out, and only for markup you produced yourself.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine added in v0.1.23

type Engine struct {
	// contains filtered or unexported fields
}

Engine is a simple wrapper around the Go templating engine.

Example
package main

import (
	"bytes"
	"fmt"
	"testing/fstest"

	"github.com/andygeiss/cloud-native-utils/templating"
)

func main() {
	fsys := fstest.MapFS{
		"page.tmpl": &fstest.MapFile{Data: []byte(`{{ define "page" }}Hello {{ .Name }}{{ end }}`)},
	}

	engine := templating.NewEngine(fsys)
	engine.Parse("*.tmpl")

	// Data is escaped for the context it lands in.
	var out bytes.Buffer
	_ = engine.Render(&out, "page", struct{ Name string }{Name: "<b>Alice</b>"})

	fmt.Println(out.String())
}
Output:
Hello &lt;b&gt;Alice&lt;/b&gt;

func NewEngine added in v0.1.23

func NewEngine(efs fs.FS) *Engine

NewEngine creates a new templating engine.

func (*Engine) Parse added in v0.1.23

func (a *Engine) Parse(patterns ...string)

Parse parses the templates in the given path.

func (*Engine) Render added in v0.1.23

func (a *Engine) Render(w io.Writer, name string, data any) error

Render renders the template with the given name and data.

func (*Engine) View added in v0.4.7

func (a *Engine) View(name string, data any) http.HandlerFunc

View returns an http.HandlerFunc that renders the template with the given name and data.

Jump to

Keyboard shortcuts

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