For more information about the template syntax please refer to the html/template and text/template package godocs. Another great resource is also the Hashicorp's Learn Go Template Syntax tutorial.
A common task when creating custom routes or emails is the need of generating HTML output.
There are plenty of Go template-engines available that you can use for this, but often for simple cases
the Go standard library html/template package should work just fine.
To make it slightly easier to load template files concurrently and on the fly, PocketBase also provides a
thin wrapper around the standard library in the
github.com/pocketbase/pocketbase/tools/template
utility package.
The general flow when working with composed and nested templates is that you create "base" template(s)
that defines various placeholders using the
{`{{template "placeholderName" .}}`} or
{`{{block "placeholderName" .}}default...{{end}}`} actions.
Then in the partials, you define the content for those placeholders using the
{`{{define "placeholderName"}}custom...{{end}}`} action.
The dot object (.) in the above represents the data passed to the templates
via the Render(data) method.
By default the templates apply contextual (HTML, JS, CSS, URI) auto escaping so the generated template
content should be injection-safe. To render raw/verbatim trusted content in the templates you can use the
builtin raw function (e.g. {`{{.content|raw}}`}).
For more information about the template syntax please refer to the html/template and text/template package godocs. Another great resource is also the Hashicorp's Learn Go Template Syntax tutorial.
Consider the following app directory structure:
We define the content for layout.html as:
We define the content for hello.html as:
Then to output the final page, we'll register a custom /hello/:name route: