package tpl

import "github.com/gohugoio/hugo/tpl"

Package tpl contains template functions and related types.

Index

Constants

const (
	HugoDeferredTemplatePrefix = "__hdeferred/"
	HugoDeferredTemplateSuffix = "__d="
)
const TemplateVersion = 2

Increments on breaking changes.

Variables

var Context = struct {
	DependencyManagerScopedProvider    hcontext.ContextDispatcher[identity.DependencyManagerScopedProvider]
	GetDependencyManagerInCurrentScope func(context.Context) identity.Manager
	DependencyScope                    hcontext.ContextDispatcher[int]
	Page                               hcontext.ContextDispatcher[page]
	IsInGoldmark                       hcontext.ContextDispatcher[bool]
}{
	DependencyManagerScopedProvider: hcontext.NewContextDispatcher[identity.DependencyManagerScopedProvider](contextKey("DependencyManagerScopedProvider")),
	DependencyScope:                 hcontext.NewContextDispatcher[int](contextKey("DependencyScope")),
	Page:                            hcontext.NewContextDispatcher[page](contextKey("Page")),
	IsInGoldmark:                    hcontext.NewContextDispatcher[bool](contextKey("IsInGoldmark")),
}

Context manages values passed in the context to templates.

var DefaultParseConfig = ParseConfig{
	Version: TemplateVersion,
}
var DefaultParseInfo = ParseInfo{
	Config: DefaultParseConfig,
}

Functions

func StripHTML

func StripHTML(s string) string

StripHTML strips out all HTML tags in s.

Types

type DeferredExecution

type DeferredExecution struct {
	Mu           sync.Mutex
	Ctx          context.Context
	TemplateName string
	Data         any

	Executed bool
	Result   string
}

type FileInfo

type FileInfo interface {
	Name() string
	Filename() string
}

type Info

type Info interface {
	ParseInfo() ParseInfo
}

type IsInternalTemplateProvider

type IsInternalTemplateProvider interface {
	IsInternalTemplate() bool
}

type ParseConfig

type ParseConfig struct {
	Version int
}

type ParseInfo

type ParseInfo struct {
	// Set for shortcode templates with any {{ .Inner }}
	IsInner bool

	// Set for partials with a return statement.
	HasReturn bool

	// Config extracted from template.
	Config ParseConfig
}

func (ParseInfo) IsZero

func (info ParseInfo) IsZero() bool

type RenderingContext

type RenderingContext struct {
	Site       site
	SiteOutIdx int
}

type Template

type Template interface {
	Name() string
	Prepare() (*texttemplate.Template, error)
}

Template is the common interface between text/template and html/template.

func AddIdentity

func AddIdentity(t Template) Template

AddIdentity checks if t is an identity.Identity and returns it if so. Else it wraps it in a templateIdentity using its name as the base.

type TemplateDebugger

type TemplateDebugger interface {
	Debug()
}

TemplateDebugger prints some debug info to stdout.

type TemplateExecutor

type TemplateExecutor interface {
	ExecuteWithContext(ctx context.Context, t Template, wr io.Writer, data any) error
}

type TemplateFinder

type TemplateFinder interface {
	TemplateLookup
	TemplateLookupVariant
}

TemplateFinder finds templates.

type TemplateFuncGetter

type TemplateFuncGetter interface {
	GetFunc(name string) (reflect.Value, bool)
}

TemplateFuncGetter allows to find a template func by name.

type TemplateHandler

type TemplateHandler interface {
	TemplateFinder
	TemplateExecutor
	LookupLayout(d layouts.LayoutDescriptor, f output.Format) (Template, bool, error)
	HasTemplate(name string) bool
	GetIdentity(name string) (identity.Identity, bool)
}

TemplateHandler finds and executes templates.

type TemplateHandlers

type TemplateHandlers struct {
	Tmpl    TemplateHandler
	TxtTmpl TemplateParseFinder
}

TemplateHandlers holds the templates needed by Hugo.

type TemplateLookup

type TemplateLookup interface {
	Lookup(name string) (Template, bool)
}

type TemplateLookupVariant

type TemplateLookupVariant interface {
	// TODO(bep) this currently only works for shortcodes.
	// We may unify and expand this variant pattern to the
	// other templates, but we need this now for the shortcodes to
	// quickly determine if a shortcode has a template for a given
	// output format.
	// It returns the template, if it was found or not and if there are
	// alternative representations (output format, language).
	// We are currently only interested in output formats, so we should improve
	// this for speed.
	LookupVariant(name string, variants TemplateVariants) (Template, bool, bool)
	LookupVariants(name string) []Template
}

type TemplateManager

type TemplateManager interface {
	TemplateHandler
	TemplateFuncGetter
	AddTemplate(name, tpl string) error
}

TemplateManager manages the collection of templates.

type TemplateParseFinder

type TemplateParseFinder interface {
	TemplateParser
	TemplateFinder
}

TemplateParseFinder provides both parsing and finding.

type TemplateParser

type TemplateParser interface {
	Parse(name, tpl string) (Template, error)
}

TemplateParser is used to parse ad-hoc templates, e.g. in the Resource chain.

type TemplateVariants

type TemplateVariants struct {
	Language     string
	OutputFormat output.Format
}

TemplateVariants describes the possible variants of a template. All of these may be empty.

type TemplatesProvider

type TemplatesProvider interface {
	Tmpl() TemplateHandler
	TextTmpl() TemplateParseFinder
}

TemplatesProvider as implemented by deps.Deps.

type UnusedTemplatesProvider

type UnusedTemplatesProvider interface {
	UnusedTemplates() []FileInfo
}

UnusedTemplatesProvider lists unused templates if the build is configured to track those.

Source Files

template.go template_info.go

Directories

PathSynopsis
tpl/castPackage cast provides template functions for data type conversions.
tpl/collectionsPackage collections provides template functions for manipulating collections such as arrays, maps, and slices.
tpl/comparePackage compare provides template functions for comparing values.
tpl/cryptoPackage crypto provides template functions for cryptographic operations.
tpl/css
tpl/dataPackage data provides template functions for working with external data sources.
tpl/debugPackage debug provides template functions to help debugging templates.
tpl/diagramsPackage diagrams provides template functions for generating diagrams.
tpl/encodingPackage encoding provides template functions for encoding content.
tpl/fmtPackage fmt provides template functions for formatting strings.
tpl/hashPackage hash provides non-cryptographic hash functions for template use.
tpl/hugoPackage hugo provides template functions for accessing the Site Hugo object.
tpl/imagesPackage images provides template functions for manipulating images.
tpl/inflectPackage inflect provides template functions for the inflection of words.
tpl/internal
tpl/jsPackage js provides functions for building JavaScript resources
tpl/langPackage lang provides template functions for content internationalization.
tpl/mathPackage math provides template functions for mathematical operations.
tpl/openapiPackage openapi provides functions for generating OpenAPI (Swagger) documentation.
tpl/openapi/openapi3Package openapi3 provides functions for generating OpenAPI v3 (Swagger) documentation.
tpl/osPackage os provides template functions for interacting with the operating system.
tpl/pagePackage page provides template functions for accessing the current Page object, the entry level context for the current template.
tpl/partialsPackage partials provides template functions for working with reusable templates.
tpl/pathPackage path provides template functions for manipulating paths.
tpl/reflectPackage reflect provides template functions for run-time object reflection.
tpl/resourcesPackage resources provides template functions for working with resources.
tpl/safePackage safe provides template functions for escaping untrusted content or encapsulating trusted content.
tpl/sitePackage site provides template functions for accessing the Site object.
tpl/stringsPackage strings provides template functions for manipulating strings.
tpl/templatesPackage templates provides template functions for working with templates.
tpl/timePackage time provides template functions for measuring and displaying time.
tpl/tplimpl
tpl/transformPackage transform provides template functions for transforming content.
tpl/urlsPackage urls provides template functions to deal with URLs.
Version
v0.144.2 (latest)
Published
Feb 19, 2025
Platform
linux/amd64
Imports
15 packages
Last checked
34 minutes ago

Tools for package owners.