Ecosyste.ms: Packages

An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 2.8% on proxy.golang.org
Top 0.2% dependent packages on proxy.golang.org
Top 0.3% dependent repos on proxy.golang.org
Top 8.2% forks on proxy.golang.org
Top 1.5% docker downloads on proxy.golang.org

proxy.golang.org : 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.

Registry - Source - Documentation - JSON
purl: pkg:golang/rsc.io/tmplfunc
License: BSD-3-Clause
Latest release: about 3 years ago
First release: about 3 years ago
Namespace: rsc.io
Dependent packages: 509
Dependent repositories: 896
Stars: 142 on GitHub
Forks: 5 on GitHub
Docker dependents: 71
Docker downloads: 222,493,867
See more repository details: repos.ecosyste.ms
Last synced: 21 days ago

    Loading...
    Readme
    Loading...