Golang template engine pipelines

There are 2 template packages, text/template and html/template. They have the same interface, but the html/template package is for generating HTML output safe against code injection, and should be used instead of text/template whenever the output is HTML. Since they have the same interface but the html/template provides some extra functionality (contextual escaping of the … Read more

Go template.ExecuteTemplate include html

Convert your []byte or string to type template.HTML (documented here) p.Body = template.HTML(s) // where s is a string or []byte Then, in your template, just: {{.Body}} It will be printed without escaping. EDIT In order to be able to include HTML in you page’s body you need to change the Page type declaration: type … Read more

Calling a template with several pipeline parameters

You could register a “dict” function in your templates that you can use to pass multiple values to a template call. The call itself would then look like that: {{template “userlist” dict “Users” .MostPopular “Current” .CurrentUser}} The code for the little “dict” helper, including registering it as a template func is here: var tmpl = … Read more

Go template name

The name of the template–unsurprisingly–is to name the template. What is it good for? As long as you don’t want to refer to the template, it doesn’t really matter. But if you want to refer to it, then yes, you refer to it by its name. When would you want to refer to it? When … Read more

How to use a field of struct or variable value as template name?

Unfortunately you can’t. The syntax of the {{template}} action: {{template “name”}} The template with the specified name is executed with nil data. {{template “name” pipeline}} The template with the specified name is executed with dot set to the value of the pipeline. The name of the template to be included is a constant string, it … Read more

tech