templates

package
v0.0.0-...-edeb3c3 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package template is a thin wrapper around the standard html/template and text/template packages that implements a convenient registry to load and cache templates on the fly concurrently.

The source for this file is located in pocketbase/tools/template

Example:

registry := template.NewRegistry()

html1, err := registry.LoadFiles(
	// the files set wil be parsed only once and then cached
	"layout.html",
	"content.html",
).Render(map[string]any{"name": "John"})

html2, err := registry.LoadFiles(
	// reuse the already parsed and cached files set
	"layout.html",
	"content.html",
).Render(map[string]any{"name": "Jane"})

The source for this file is pocketbase/tools/template

Index

Constants

View Source
const (
	FILE_FORMAT        = "tmpl"
	GLOBAL_FILE_PREFIX = "_"
	END_BLOCK          = "{{end}}"
	DEFAULT_ROOT       = `` /* 189-byte string literal not displayed */

)

Variables

This section is empty.

Functions

This section is empty.

Types

type Dir

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

func (Dir) String

func (dir Dir) String() string

type Registry

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

Registry defines a templates registry that is safe to be used by multiple goroutines.

Use the Registry.Load* methods to load templates into the registry.

func NewRegistry

func NewRegistry(routes fs.FS, options RegistryOptions) *Registry

NewRegistry creates and initializes a new templates registry with some defaults (eg. global "raw" template function for unescaped HTML).

Use the Registry.Load* methods to load templates into the registry.

func (*Registry) AddFuncs

func (r *Registry) AddFuncs(funcs map[string]any) *Registry

AddFuncs registers new global template functions.

The key of each map entry is the function name that will be used in the templates. If a function with the map entry name already exists it will be replaced with the new one.

The value of each map entry is a function that must have either a single return value, or two return values of which the second has type error.

Example:

r.AddFuncs(map[string]any{
  "toUpper": func(str string) string {
      return strings.ToUppser(str)
  },
  ...
})

func (*Registry) LoadDir

func (r *Registry) LoadDir(path string) (*Template, error)

This function parses a direcory structure for dirs that contain tmpl files. We need to kindof create a dependency graph bc the sequence of documents passed into the templating engine is significant. We can have layouts or nested layouts in a hierachical structure, which get passed to subdirectories, if they don't contain any layout files themselves. Parse sequence is significant. We parse:

  1. layout.tmpl
  2. body.tmpl
  3. head.tmpl
  4. all other .tmpl files

header.tmpl files are parsed seperately into response headers (we skip the dir if no index + no body + no header is present)

func (*Registry) LoadFile

func (r *Registry) LoadFile(path string) (*Template, error)

type RegistryOptions

type RegistryOptions struct {
	Extension     string
	InhFilePrefix string
}

func DefaultRegistryOptions

func DefaultRegistryOptions() RegistryOptions

type Template

type Template struct {
	Directory *Dir
	Template  *template.Template
	Headers   *template.Template
	// contains filtered or unexported fields
}

Renderer defines a single parsed template. It's a wrapper around html/templates

func (*Template) Render

func (r *Template) Render(data any) (string, error)

Render executes the template with the specified data as the dot object and returns the result as plain string.

func (*Template) RenderPartial

func (r *Template) RenderPartial(name string, data any) (string, error)

Jump to

Keyboard shortcuts

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