template

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

README

server/template

Import path: github.com/InsideGallery/core/server/template

template wraps html/template parsing and execution for server-side HTML templates, including embedded filesystems.

Main APIs

  • Options: explicit template name, directory, file list, and fs.FS.
  • NewTemplateFromOptions(options): creates a template from explicit options.
  • NewTemplateWithDir(name, dir, source, files...): parses files from an explicit base directory.
  • NewTemplateBySource(source, name, pattern): parses an embedded template pattern.
  • Template: named template wrapper.
  • Engine: stores templates by name.
  • Engine.Add, Exists, and Execute: template registry and rendering helpers.
  • ErrExecuteTemplate and ErrNotFoundTemplate: execution sentinel errors.

GetDefaultTemplateDir, SetDefaultTemplateDir, and NewTemplate are deprecated environment-based helpers.

Usage

tmpl, err := coretemplate.NewTemplateWithDir("page.tmpl", "templates", fsys, "page.tmpl")
if err != nil {
	return err
}

engine := coretemplate.NewEngine()
engine.Add(tmpl)

html, err := engine.Execute("page.tmpl", map[string]string{"Name": "Ada"})

Operational Notes

Templates are configured with missingkey=error. Prefer explicit directory configuration through Options or NewTemplateWithDir instead of process-wide environment state.

Documentation

Overview

Package template provides server-side HTML template rendering helpers.

Prefer NewTemplateWithDir or NewTemplateFromOptions so template directory configuration is explicit.

Index

Constants

View Source
const DefaultTemplateDir = "DEFAULT_TEMPLATE_DIR"

DefaultTemplateDir have constant for template

Variables

View Source
var (
	ErrExecuteTemplate  = errors.New("error execute template")
	ErrNotFoundTemplate = errors.New("error not found template")
)

templates related errors

Functions

func GetDefaultTemplateDir deprecated

func GetDefaultTemplateDir(prefix string) string

GetDefaultTemplateDir return default template dir

Deprecated: pass the directory to NewTemplateWithDir or NewTemplateFromOptions.

func SetDefaultTemplateDir deprecated

func SetDefaultTemplateDir(prefix string, d string)

SetDefaultTemplateDir set default template dir

Deprecated: pass the directory to NewTemplateWithDir or NewTemplateFromOptions.

Types

type Engine

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

Engine type template engine

func NewEngine

func NewEngine() *Engine

NewEngine return engine

func (*Engine) Add

func (e *Engine) Add(t *Template)

Add add template

func (*Engine) Execute

func (e *Engine) Execute(name string, data interface{}) ([]byte, error)

Execute execute current template and return parsed strings

func (*Engine) Exists

func (e *Engine) Exists(name string) bool

Exists return true if page exists

type Options added in v1.1.0

type Options struct {
	Name  string
	Dir   string
	Files []string
	FS    fs.FS
}

Options configures template parsing without environment state.

type Template

type Template struct {
	*template.Template
	Name string
}

Template describe template

func NewTemplate deprecated

func NewTemplate(name, prefix string, source fs.FS, files ...string) (*Template, error)

NewTemplate return new template

Deprecated: use NewTemplateWithDir or NewTemplateFromOptions with explicit directory config.

func NewTemplateBySource

func NewTemplateBySource(source embed.FS, name, pattern string) (*Template, error)

NewTemplateBySource return new template bu source

func NewTemplateFromOptions added in v1.1.0

func NewTemplateFromOptions(options Options) (*Template, error)

NewTemplateFromOptions returns a new template from explicit options.

func NewTemplateWithDir added in v1.1.0

func NewTemplateWithDir(name, dir string, source fs.FS, files ...string) (*Template, error)

NewTemplateWithDir returns a new template using an explicit base directory.

Jump to

Keyboard shortcuts

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