Package Usage: go: rsc.io/tmplfunc
Package tmplfunc provides an extension of Go templates
in which templates can be invoked as if they were functions.
For example, after parsing
this package installs a function named link allowing the template
to be invoked as
instead of the longer-form (assuming an appropriate function named dict)
The function installed for a given template depends on the name of the
defined template, which can include not just a function name but also
a list of parameter names. The function name and parameter names must
consist only of letters, digits, and underscores, with a leading non-digit.
If there is no parameter list, then the function is expected to take
at most one argument, made available in the template body as “.” (dot).
If such a function is called with no arguments, dot will be a nil interface value.
If there is a parameter list, then the function requires an argument for
each parameter, except for optional and variadic parameters, explained below.
Inside the template, the top-level value “.” is a map[string]interface{} in which
each parameter name is mapped to the corresponding argument value.
A parameter x can therefore be accessed as {{(index . "x")}} or, more concisely, {{.x}}.
The first special case in parameter handling is that
a parameter can be made optional by adding a “?” suffix after its name.
If the argument list ends before that parameter, the corresponding map entry
will be present and set to a nil value.
The second special case is that a parameter can be made variadic
by adding a “...” suffix after its name.
The corresponding map entry contains a []interface{} holding the
zero or more arguments corresponding to that parameter.
In the parameter list, required parameters must precede optional parameters,
which must in turn precede any variadic parameter.
For example, we can revise the link template given earlier to make the
link text optional, substituting the URL when the text is omitted:
This package is meant to be used with templates from either the
text/template or html/template packages. Given a *template.Template
variable t, substitute:
Parse, ParseFiles, and ParseGlob parse the new templates but also add
functions that invoke them, named according to the function signatures.
Templates can only invoke functions for templates that have already been
defined or that are being defined in the same Parse, ParseFiles, or ParseGlob call.
For example, templates in two files x.tmpl and y.tmpl can call each other
only if ParseFiles or ParseGlob is used to parse both files in a single call.
Otherwise, the parsing of the first file will report that calls to templates in
the second file are calling unknown functions.
When used with the html/template package, all function-invoked template
calls are treated as invoking templates producing HTML. In order to use a
template that produces some other kind of text fragment, the template must
be invoked directly using the {{template "name"}} form, not as a function call.
3 versions
Latest release: about 4 years ago
509 dependent packages
View more package details: https://packages.ecosystem.code.gouv.fr/registries/proxy.golang.org/packages/rsc.io/tmplfunc