Documentation
¶
Overview ¶
Package views holds the template engines of the Iris web framework: HTML, Django, Handlebars, Jet, Pug and Ace.
Every engine satisfies the three interfaces of github.com/kataras/iris/v14/middleware/view (Engine, EngineFuncer and EngineExtenser), so it is registered with view.Handler and rendered with view.Render:
templates := views.HTML("./views", ".html")
app.Use(view.Handler(templates))
app.Get("/", func(ctx iris.Context) {
view.Render(ctx, "index.html", "layouts/main.html", iris.Map{"Title": "Home"})
})
Loading ¶
The six engines load their templates the same way:
- Load parses every template and returns the first error. Call it at startup to refuse to start with a template that does not parse.
- An engine that was never loaded loads itself on its first ExecuteTemplate. Requests that arrive together wait for that one load. If it fails, every render returns its error until a Load succeeds.
- Reload(true) parses the templates again on every render, for development.
A template or a layout that does not exist is a view.ErrTemplateNotFound. A name may leave the engine's extension out and may start with a slash.
Index ¶
- Constants
- Variables
- func AddJetRuntimeVars(ctx *context.Context, jetVarMap JetRuntimeVars)
- type AceEngine
- type DjangoEngine
- func (s *DjangoEngine) AddFilter(filterName string, filterBody FilterFunction) *DjangoEngine
- func (s *DjangoEngine) AddFunc(funcName string, funcBody any)
- func (s *DjangoEngine) ExecuteTemplate(w io.Writer, filename string, _ string, bindingData any) error
- func (s *DjangoEngine) Ext() string
- func (s *DjangoEngine) IsLoaded() bool
- func (s *DjangoEngine) Load() error
- func (s *DjangoEngine) Name() string
- func (s *DjangoEngine) ParseTemplate(name string, contents []byte) error
- func (s *DjangoEngine) RegisterFilter(filterName string, filterBody FilterFunction) *DjangoEngine
- func (s *DjangoEngine) RegisterTag(tagName string, fn TagParser) error
- func (s *DjangoEngine) Reload(developmentMode bool) *DjangoEngine
- func (s *DjangoEngine) RootDir(root string) *DjangoEngine
- type Engine
- type ErrTemplateNotFound
- type Error
- type FilterFunction
- type HTMLEngine
- func (s *HTMLEngine) AddFunc(funcName string, funcBody any)
- func (s *HTMLEngine) AddLayoutFunc(funcName string, funcBody any) *HTMLEngine
- func (s *HTMLEngine) Delims(left, right string) *HTMLEngine
- func (s *HTMLEngine) ExecuteTemplate(w io.Writer, name string, layout string, bindingData any) error
- func (s *HTMLEngine) Ext() string
- func (s *HTMLEngine) FS(dirOrFS any) *HTMLEngine
- func (s *HTMLEngine) Funcs(funcMap template.FuncMap) *HTMLEngine
- func (s *HTMLEngine) IsLoaded() bool
- func (s *HTMLEngine) Layout(layoutFile string) *HTMLEngine
- func (s *HTMLEngine) Load() error
- func (s *HTMLEngine) Name() string
- func (s *HTMLEngine) Option(opt ...string) *HTMLEngine
- func (s *HTMLEngine) ParseTemplate(name string, contents []byte, funcs template.FuncMap) (err error)
- func (s *HTMLEngine) Reload(developmentMode bool) *HTMLEngine
- func (s *HTMLEngine) RootDir(root string) *HTMLEngine
- func (s *HTMLEngine) SetFuncs(funcMap template.FuncMap) *HTMLEngine
- type HandlebarsEngine
- func (s *HandlebarsEngine) AddFunc(funcName string, funcBody any)
- func (s *HandlebarsEngine) AddGlobalFunc(funcName string, funcBody any)
- func (s *HandlebarsEngine) ExecuteTemplate(w io.Writer, filename string, layout string, bindingData any) error
- func (s *HandlebarsEngine) Ext() string
- func (s *HandlebarsEngine) IsLoaded() bool
- func (s *HandlebarsEngine) Layout(layoutFile string) *HandlebarsEngine
- func (s *HandlebarsEngine) Load() error
- func (s *HandlebarsEngine) Name() string
- func (s *HandlebarsEngine) ParseTemplate(name string, contents string, funcs template.FuncMap) error
- func (s *HandlebarsEngine) Reload(developmentMode bool) *HandlebarsEngine
- func (s *HandlebarsEngine) RootDir(root string) *HandlebarsEngine
- type INodeTag
- type JetArguments
- type JetEngine
- func (s *JetEngine) AddFunc(funcName string, funcBody any)
- func (s *JetEngine) AddRuntimeVars(ctx *context.Context, vars JetRuntimeVars)
- func (s *JetEngine) AddVar(key string, value any)
- func (s *JetEngine) Delims(left, right string) *JetEngine
- func (s *JetEngine) ExecuteTemplate(w io.Writer, filename string, layout string, bindingData any) error
- func (s *JetEngine) Ext() string
- func (s *JetEngine) IsLoaded() bool
- func (s *JetEngine) Load() error
- func (s *JetEngine) Name() string
- func (s *JetEngine) ParseTemplate(name string, contents string) error
- func (s *JetEngine) Reload(developmentMode bool) *JetEngine
- func (s *JetEngine) RootDir(root string) *JetEngine
- func (s *JetEngine) SetLoader(loader jet.Loader) *JetEngine
- func (s *JetEngine) String() string
- type JetRuntime
- type JetRuntimeVars
- type Parser
- type TagParser
- type Token
- type Value
Constants ¶
const JetRuntimeVarsContextKey = "iris.jetvarmap"
JetRuntimeVarsContextKey is the Iris Context key to keep any custom jet runtime variables. See `AddJetRuntimeVars` package-level function and `JetEngine.AddRuntimeVars` method.
const NoLayout = "iris.nolayout"
NoLayout disables the configuration's layout for a specific execution.
Variables ¶
var AsSafeValue = pongo2.AsSafeValue
AsSafeValue works like AsValue, but does not apply the 'escape' filter. Shortcut for `pongo2.AsSafeValue`.
var AsValue = pongo2.AsValue
AsValue converts any given value to a pongo2.Value Usually being used within own functions passed to a template through a Context or within filter functions.
Example:
AsValue("my string")
Shortcut for `pongo2.AsValue`.
var ErrNoTemplates = errors.New("views: no templates found")
ErrNoTemplates is returned by Load, and by the first render of an engine that was never loaded, when there is nothing to load: no file with the engine's extension and no template added with ParseTemplate. The usual causes are a wrong directory and a wrong extension.
Functions ¶
func AddJetRuntimeVars ¶
func AddJetRuntimeVars(ctx *context.Context, jetVarMap JetRuntimeVars)
AddJetRuntimeVars sets or inserts runtime jet variables through the Iris Context. This gives the ability to add runtime variables from different handlers in the request chain, something that the jet template parser does not offer at all.
Usage: views.AddJetRuntimeVars(ctx, views.JetRuntimeVars{...}). See `JetEngine.AddRuntimeVars` too.
Types ¶
type AceEngine ¶
type AceEngine struct {
*HTMLEngine
// contains filtered or unexported fields
}
AceEngine represents the Ace view engine. See the `Ace` package-level function for more.
func Ace ¶
Ace returns a new Ace view engine. It shares the same exactly logic with the html view engine, it uses the same exactly configuration. The given "extension" MUST begin with a dot. Ace minifies the response automatically unless SetIndent() method is set.
Every file is compiled on its own into html/template text, so the actions of the html engine ({{ yield . }}, {{ render "name" . }} and the rest) and its layouts work the same. The "= include" helper of Ace becomes a {{ template }} action: give it the name of the file with its extension, as in "= include partials/footer.ace .". The base and inner templates of Ace ("= yield" and "= content") are not available, use a layout instead.
Read more about the Ace Go Parser: https://github.com/yosssi/ace
Usage: Ace("./views", ".ace") or Ace(embed.FS, ".ace").RootDir("views") for embedded templates.
type DjangoEngine ¶
type DjangoEngine struct {
// Set is the pongo2 template set of the engine.
// It is nil until the first Load, ParseTemplate or render, unless the
// application assigns one of its own before that.
Set *pongo2.TemplateSet
// contains filtered or unexported fields
}
DjangoEngine contains the django view engine structure.
func Django ¶
func Django(fs any, extension string) *DjangoEngine
Django creates and returns a new django view engine. The given "extension" MUST begin with a dot.
A Django template names its own parent with {% extends %}, so the layout argument of ExecuteTemplate is ignored by this engine.
Usage: Django("./views", ".html") or Django(embed.FS, ".html").RootDir("views") for embedded templates.
func (*DjangoEngine) AddFilter ¶
func (s *DjangoEngine) AddFilter(filterName string, filterBody FilterFunction) *DjangoEngine
AddFilter registers a new filter.
Same as `RegisterFilter`.
func (*DjangoEngine) AddFunc ¶
func (s *DjangoEngine) AddFunc(funcName string, funcBody any)
AddFunc adds the function to the template's Globals. A template calls it as {{ name(argument) }}. Unlike the other engines, it takes effect at once, loaded or not.
func (*DjangoEngine) ExecuteTemplate ¶
func (s *DjangoEngine) ExecuteTemplate(w io.Writer, filename string, _ string, bindingData any) error
ExecuteTemplate executes a template and writes its result to the w writer. The layout is ignored: a Django template names its own parent with {% extends %}.
The engine loads itself first if it was never loaded. It returns a view.ErrTemplateNotFound for a template that does not exist. Nothing is written to w when the execution fails.
func (*DjangoEngine) Ext ¶
func (s *DjangoEngine) Ext() string
Ext returns the file extension which this view engine is responsible to render. If the filename extension on ExecuteTemplate is empty then this is appended.
func (*DjangoEngine) IsLoaded ¶
func (s *DjangoEngine) IsLoaded() bool
IsLoaded reports whether the templates are loaded at least once or not.
func (*DjangoEngine) Load ¶
func (s *DjangoEngine) Load() error
Load parses the templates to the engine.
Calling it is optional: an engine that was never loaded loads itself on its first ExecuteTemplate. Call it at startup to find a template that does not parse before the first request does.
It may be called again to parse everything anew. If it fails then, the templates of the previous load keep rendering.
Returns an error if something bad happens, user is responsible to catch it.
func (*DjangoEngine) Name ¶
func (s *DjangoEngine) Name() string
Name returns the django engine's name.
func (*DjangoEngine) ParseTemplate ¶
func (s *DjangoEngine) ParseTemplate(name string, contents []byte) error
ParseTemplate adds a custom template from text. This parser does not support funcs per template. Use the `AddFunc` instead.
A template given here is kept, and parsed again on every load. When it returns an error, nothing was added.
func (*DjangoEngine) RegisterFilter ¶
func (s *DjangoEngine) RegisterFilter(filterName string, filterBody FilterFunction) *DjangoEngine
RegisterFilter registers a new filter.
pongo2 keeps its filters in one registry for the whole process. A filter is therefore shared by every Django engine of the program, and a name that is registered again replaces the filter for all of them. Register filters at startup, before anything renders: the registry is not safe to change while templates execute.
See http://www.florian-schlachter.de/post/pongo2/ for more about writing filters and tags.
func (*DjangoEngine) RegisterTag ¶
func (s *DjangoEngine) RegisterTag(tagName string, fn TagParser) error
RegisterTag registers a new tag. You usually want to call this function in the tag's init() function: http://golang.org/doc/effective_go.html#init
Tags live in the process-wide registry of pongo2, like filters, and it returns an error for a name that is already registered.
See http://www.florian-schlachter.de/post/pongo2/ for more about writing filters and tags.
func (*DjangoEngine) Reload ¶
func (s *DjangoEngine) Reload(developmentMode bool) *DjangoEngine
Reload if set to true the templates are reloading on each render, use it when you're in development and you don't want to restart the whole app when you edit a template file.
Every render then parses all templates first, one render at a time. Do not use it in production.
func (*DjangoEngine) RootDir ¶
func (s *DjangoEngine) RootDir(root string) *DjangoEngine
RootDir sets the directory to be used as a starting point to load templates from the provided file system. It is what points the engine at a directory of an embed.FS.
It panics if root is not a valid path inside the file system.
type Engine ¶
Engine is the interface for a compatible Iris view engine. It's an alias of view.Engine.
type ErrTemplateNotFound ¶
type ErrTemplateNotFound = view.ErrTemplateNotFound
ErrTemplateNotFound reports whether a template was not found in the parsed templates tree.
type FilterFunction ¶
type FilterFunction = pongo2.FilterFunction
FilterFunction type alias for pongo2.FilterFunction
type HTMLEngine ¶
type HTMLEngine struct {
// Templates is a copy of the template set of the last successful load,
// nil before it. Use it to look templates up, for example with
// DefinedTemplates. The engine does not render with it, so parsing into
// it or changing its functions has no effect on what the engine renders.
//
// Every load replaces it, with no lock: read it when no load can run.
// With Reload(true) every render is a load.
Templates *template.Template
// contains filtered or unexported fields
}
HTMLEngine contains the html view engine structure. Read more at: https://pkg.go.dev/text/template#hdr-Functions.
It is also the engine behind Pug and Ace, which turn their own syntax into html/template text before it is parsed.
func HTML ¶
func HTML(dirOrFS any, extension string) *HTMLEngine
HTML creates and returns a new html view engine. The html engine used like the "html/template" standard go package but with a lot of extra features. The given "extension" MUST begin with a dot.
Usage: HTML("./views", ".html") or HTML(embed.FS, ".html").RootDir("views") for embedded templates or HTML(nil, ".html") and ParseTemplate("hello.html", []byte("hello {{.Name}}"), nil) for templates given as text.
func Pug ¶
func Pug(fs any, extension string) *HTMLEngine
Pug (or Jade) returns a new pug view engine. It shares the same exactly logic with the html view engine, it uses the same exactly configuration. The given "extension" MUST begin with a dot.
A template is turned into html/template text first, so the actions of the html engine ({{ yield . }}, {{ render "name" . }} and the rest) and its layouts work the same. The "include" and "extends" keywords of Pug read from the engine's own file system. Give them the file extension, as in "include partials/footer.pug".
Read more about the Jade Go Parser: https://github.com/Joker/jade
Usage: Pug("./views", ".pug") or Pug(embed.FS, ".pug").RootDir("views") for embedded templates.
func (*HTMLEngine) AddFunc ¶
func (s *HTMLEngine) AddFunc(funcName string, funcBody any)
AddFunc adds the function to the template's function map. It is legal to overwrite elements of the default actions: - render func(fullPartialName string, binding any) (template.HTML, error).
html/template resolves function names when it parses, so the function takes effect at the next load: add it before Load, or before the first render of an engine that loads itself.
func (*HTMLEngine) AddLayoutFunc ¶
func (s *HTMLEngine) AddLayoutFunc(funcName string, funcBody any) *HTMLEngine
AddLayoutFunc adds the function to the template's function map. It is legal to overwrite elements of the default layout actions: - current func() string - partial func(partialName string, binding any) (template.HTML, error) - partial_r func(partialName string, binding any) (template.HTML, error) - part func(partName string, binding any) (template.HTML, error) - render func(fullPartialName string, binding any) (template.HTML, error).
The function takes effect at the next load. Functions belong to the whole template set in html/template, so pages can call it too, and a function of the same name added with AddFunc is replaced by it. The yield action always renders the current page and cannot be replaced.
func (*HTMLEngine) Delims ¶
func (s *HTMLEngine) Delims(left, right string) *HTMLEngine
Delims sets the action delimiters to the specified strings, to be used in templates. An empty delimiter stands for the corresponding default: {{ or }}.
func (*HTMLEngine) ExecuteTemplate ¶
func (s *HTMLEngine) ExecuteTemplate(w io.Writer, name string, layout string, bindingData any) error
ExecuteTemplate executes a template and writes its result to the w writer. The layout is the layout argument or, when that is empty, the one set by Layout. NoLayout renders the template without any.
The engine loads itself first if it was never loaded. It returns a view.ErrTemplateNotFound for a template or a layout that does not exist. Nothing is written to w when the execution fails.
func (*HTMLEngine) Ext ¶
func (s *HTMLEngine) Ext() string
Ext returns the file extension which this view engine is responsible to render. If the filename extension on ExecuteTemplate is empty then this is appended.
func (*HTMLEngine) FS ¶
func (s *HTMLEngine) FS(dirOrFS any) *HTMLEngine
FS sets the directory or the file system the templates are loaded from, replacing the one given to the constructor.
func (*HTMLEngine) Funcs ¶
func (s *HTMLEngine) Funcs(funcMap template.FuncMap) *HTMLEngine
Funcs adds the elements of the argument map to the template's function map. It is legal to overwrite elements of the map. The return value is the template, so calls can be chained.
func (*HTMLEngine) IsLoaded ¶
func (s *HTMLEngine) IsLoaded() bool
IsLoaded reports whether the templates are loaded at least once or not.
func (*HTMLEngine) Layout ¶
func (s *HTMLEngine) Layout(layoutFile string) *HTMLEngine
Layout sets the layout template file which inside should use the {{ yield . }} func to yield the main template file and optionally {{partial/partial_r/render . }} to render other template files like headers and footers
The 'tmplLayoutFile' is a relative path of the templates base directory, for the template file with its extension.
Example: HTML("./templates", ".html").Layout("layouts/mainLayout.html")
// mainLayout.html is inside: "./templates/layouts/".
Note: Layout can be changed for a specific call with the layout argument of view.Render, or with view.Layout and view.WithLayout. Pass NoLayout there to render a template without this layout.
func (*HTMLEngine) Load ¶
func (s *HTMLEngine) Load() error
Load parses the templates to the engine. It's also responsible to add the necessary global functions.
Calling it is optional: an engine that was never loaded loads itself on its first ExecuteTemplate. Call it at startup to find a template that does not parse before the first request does.
It may be called again to parse everything anew. If it fails then, the templates of the previous load keep rendering.
Returns an error if something bad happens, caller is responsible to handle that.
func (*HTMLEngine) Option ¶
func (s *HTMLEngine) Option(opt ...string) *HTMLEngine
Option sets options for the template. Options are described by strings, either a simple string or "key=value". There can be at most one equals sign in an option string. An option string that is unrecognized or otherwise invalid makes Load return an error.
Known options:
missingkey: Control the behavior during execution if a map is indexed with a key that is not present in the map.
"missingkey=default" or "missingkey=invalid" The default behavior: Do nothing and continue execution. If printed, the result of the index operation is the string "<no value>". "missingkey=zero" The operation returns the zero value for the map type's element. "missingkey=error" Execution stops immediately with an error.
func (*HTMLEngine) ParseTemplate ¶
func (s *HTMLEngine) ParseTemplate(name string, contents []byte, funcs template.FuncMap) (err error)
ParseTemplate adds a custom template to the root template. The funcs are for this template only as far as parsing goes: html/template keeps one function map for the whole set.
A template given here is kept, and parsed again on every load. When it returns an error, nothing was added.
func (*HTMLEngine) Reload ¶
func (s *HTMLEngine) Reload(developmentMode bool) *HTMLEngine
Reload if set to true the templates are reloading on each render, use it when you're in development and you don't want to restart the whole app when you edit a template file.
Every render then parses all templates first, one render at a time. Do not use it in production.
func (*HTMLEngine) RootDir ¶
func (s *HTMLEngine) RootDir(root string) *HTMLEngine
RootDir sets the directory to be used as a starting point to load templates from the provided file system. It is what points the engine at a directory of an embed.FS.
It panics if root is not a valid path inside the file system.
func (*HTMLEngine) SetFuncs ¶
func (s *HTMLEngine) SetFuncs(funcMap template.FuncMap) *HTMLEngine
SetFuncs overrides the template funcs with the given "funcMap".
type HandlebarsEngine ¶
type HandlebarsEngine struct {
// contains filtered or unexported fields
}
HandlebarsEngine contains the handlebars view engine structure.
func Handlebars ¶
func Handlebars(fs any, extension string) *HandlebarsEngine
Handlebars creates and returns a new handlebars view engine. The given "extension" MUST begin with a dot.
Usage: Handlebars("./views", ".html") or Handlebars(embed.FS, ".html").RootDir("views") for embedded templates.
func (*HandlebarsEngine) AddFunc ¶
func (s *HandlebarsEngine) AddFunc(funcName string, funcBody any)
AddFunc adds a helper to the templates of this engine. It is legal to overwrite elements of the default actions: - render func(fullPartialName string, binding any) raymond.SafeString.
A helper is given to a template when it is parsed, so it takes effect at the next load: add it before Load, or before the first render of an engine that loads itself.
func (*HandlebarsEngine) AddGlobalFunc ¶
func (s *HandlebarsEngine) AddGlobalFunc(funcName string, funcBody any)
AddGlobalFunc registers a global template function for all Handlebars view engines. A name that is registered already is replaced.
It panics, as raymond does, if funcBody is not a function with one result.
func (*HandlebarsEngine) ExecuteTemplate ¶
func (s *HandlebarsEngine) ExecuteTemplate(w io.Writer, filename string, layout string, bindingData any) error
ExecuteTemplate executes a template and writes its result to the w writer. The layout is the layout argument or, when that is empty, the one set by Layout. NoLayout renders the template without any. A render through a layout needs the data to be a map[string]any or nil.
The engine loads itself first if it was never loaded. It returns a view.ErrTemplateNotFound for a template or a layout that does not exist. Nothing is written to w when the execution fails.
func (*HandlebarsEngine) Ext ¶
func (s *HandlebarsEngine) Ext() string
Ext returns the file extension which this view engine is responsible to render. If the filename extension on ExecuteTemplate is empty then this is appended.
func (*HandlebarsEngine) IsLoaded ¶
func (s *HandlebarsEngine) IsLoaded() bool
IsLoaded reports whether the templates are loaded at least once or not.
func (*HandlebarsEngine) Layout ¶
func (s *HandlebarsEngine) Layout(layoutFile string) *HandlebarsEngine
Layout sets the layout template file, which prints the page where it says {{ yield }} and may use {{ render "name" . }} to render other template files like headers and footers.
The page reaches the layout as a "yield" entry of the template data, so a render through a layout needs the data to be a map[string]any or nil.
func (*HandlebarsEngine) Load ¶
func (s *HandlebarsEngine) Load() error
Load parses the templates to the engine.
Calling it is optional: an engine that was never loaded loads itself on its first ExecuteTemplate. Call it at startup to find a template that does not parse before the first request does.
It may be called again to parse everything anew. If it fails then, the templates of the previous load keep rendering.
Returns an error if something bad happens, user is responsible to catch it.
func (*HandlebarsEngine) Name ¶
func (s *HandlebarsEngine) Name() string
Name returns the handlebars engine's name.
func (*HandlebarsEngine) ParseTemplate ¶
func (s *HandlebarsEngine) ParseTemplate(name string, contents string, funcs template.FuncMap) error
ParseTemplate adds a custom template from text. The funcs are helpers for this template alone.
A template given here is kept, and parsed again on every load. When it returns an error, nothing was added.
func (*HandlebarsEngine) Reload ¶
func (s *HandlebarsEngine) Reload(developmentMode bool) *HandlebarsEngine
Reload if set to true the templates are reloading on each render, use it when you're in development and you don't want to restart the whole app when you edit a template file.
Every render then parses all templates first, one render at a time. Do not use it in production.
func (*HandlebarsEngine) RootDir ¶
func (s *HandlebarsEngine) RootDir(root string) *HandlebarsEngine
RootDir sets the directory to be used as a starting point to load templates from the provided file system. It is what points the engine at a directory of an embed.FS.
It panics if root is not a valid path inside the file system.
type JetArguments ¶
JetArguments is a type alias of `jet.Arguments`, can be used on `AddFunc$funcBody`.
type JetEngine ¶
type JetEngine struct {
// The Set is the `*jet.Set`, exported to offer any custom capabilities that jet users may want.
// It is nil until the first Load, ParseTemplate or render.
Set *jet.Set
// contains filtered or unexported fields
}
JetEngine is the jet template parser's view engine.
func Jet ¶
Jet creates and returns a new jet view engine. The given "extension" MUST begin with a dot. It panics for an extension other than ".jet", ".html.jet" and ".jet.html".
A Jet template names its own parent with {{ extends }}, so the layout argument of ExecuteTemplate is ignored by this engine.
Usage: Jet("./views", ".jet") or Jet(embed.FS, ".jet").RootDir("views") for embedded templates.
func (*JetEngine) AddFunc ¶
AddFunc should adds a global function to the jet template set. It takes effect at once, loaded or not.
The function can be a jet.Func, a func(JetArguments) reflect.Value, or any other Go function, which jet calls with the arguments of the template.
func (*JetEngine) AddRuntimeVars ¶
func (s *JetEngine) AddRuntimeVars(ctx *context.Context, vars JetRuntimeVars)
AddRuntimeVars sets or inserts runtime jet variables through the Iris Context. This gives the ability to add runtime variables from different handlers in the request chain, something that the jet template parser does not offer at all.
Usage: engine.AddRuntimeVars(ctx, views.JetRuntimeVars{...}). See `views.AddJetRuntimeVars` if package-level access is more meanful to the code flow.
func (*JetEngine) AddVar ¶
AddVar adds a global variable to the jet template set. It takes effect at once, loaded or not.
func (*JetEngine) Delims ¶
Delims sets the action delimiters to the specified strings, to be used in templates. An empty delimiter stands for the corresponding default: {{ or }}. Should act before `Load` or `view.Handler`.
func (*JetEngine) ExecuteTemplate ¶
func (s *JetEngine) ExecuteTemplate(w io.Writer, filename string, layout string, bindingData any) error
ExecuteTemplate should execute a template by its filename with an optional layout and bindingData. The layout is ignored: a Jet template names its own parent with {{ extends }}.
The engine loads itself first if it was never loaded. It returns a view.ErrTemplateNotFound for a template that does not exist.
func (*JetEngine) Ext ¶
Ext should return the final file extension which this view engine is responsible to render. If the filename extension on ExecuteTemplate is empty then this is appended.
func (*JetEngine) IsLoaded ¶
IsLoaded reports whether the templates are loaded at least once or not.
func (*JetEngine) Load ¶
Load parses every template, to find the ones that do not parse, and empties the cache, so that each template is read again when it is next rendered.
Calling it is optional: an engine that was never loaded loads itself on its first ExecuteTemplate. Call it at startup to find a template that does not parse before the first request does.
func (*JetEngine) ParseTemplate ¶
ParseTemplate accepts a name and contnets to parse and cache a template. This parser does not support funcs per template. Use the `AddFunc` instead.
When it returns an error, nothing was added.
func (*JetEngine) Reload ¶
Reload if setted to true the templates are reloading on each render, use it when you're in development and you don't want to restart the whole app when you edit a template file.
Every render then parses all templates first, as in the other engines, and jet is put in its development mode, in which it reads the template it renders again. Call it before the engine loads. Do not use it in production.
func (*JetEngine) RootDir ¶
RootDir sets the directory to be used as a starting point to load templates from the provided file system. It is what points the engine at a directory of an embed.FS.
It panics if root is not a valid path inside the file system.
type JetRuntime ¶
JetRuntime is a type alias of `jet.Runtime`, can be used on RuntimeVariable input function.
type JetRuntimeVars ¶
JetRuntimeVars is a type alias for `jet.VarMap`. Can be used at `AddJetRuntimeVars/JetEngine.AddRuntimeVars` to set a runtime variable ${name} to the executing template.
type TagParser ¶
TagParser the function signature of the tag's parser you will have to implement in order to create a new tag.
'doc' is providing access to the whole document while 'arguments' is providing access to the user's arguments to the tag:
{% your_tag_name some "arguments" 123 %}
start_token will be the *Token with the tag's name in it (here: your_tag_name).
Please see the Parser documentation on how to use the parser. See `RegisterTag` for more information about writing a tag as well.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
embedding-templates-into-app
command
Templates compiled into the binary, with layouts and a partial.
|
Templates compiled into the binary, with layouts and a partial. |
|
overview
command
The HTML engine: a directory of templates, rendered through the view middleware.
|
The HTML engine: a directory of templates, rendered through the view middleware. |
|
write-to
command
An engine renders into any io.Writer, with no request and no server.
|
An engine renders into any io.Writer, with no request and no server. |