Template Files
GoHT templates live in .goht files. A .goht file is mostly Go source: it can declare a package, imports, helper types, helper functions, and one or more template declarations.
Template declarations start with a language directive followed by a valid Go function declaration:
@hamldeclares a Haml template.@slimdeclares a Slim template.@egodeclares an EGO template.
GoHT generates a Go function for each template declaration. The template name must therefore be a valid Go function name, and parameters use normal Go function parameter syntax.
The generated function returns a goht.Template. A goht.Template renders with a context.Context and an io.Writer.
Inside a template, the render context is available as ctx. Template code should write output through template syntax rather than writing to the io.Writer directly.
Template directives
Use the directive that matches the template language you want to write.
Deprecated @goht
@goht is still accepted as a deprecated alias for @haml. Existing Haml templates that use @goht can migrate by changing only the declaration directive:
becomes:
Use @haml in new code and documentation examples.
Multiple templates per file
A single .goht file can contain multiple templates. Haml, Slim, and EGO templates can also coexist in the same file.
Each template must have a unique generated Go function name in the output package.
Receiver templates
Templates can also be declared as methods with Go receivers.
GoHT generates the matching Go method:
Receiver templates render the same way as receiver-less templates:
Composition directives
Template composition uses @render, @children, and @slot. Dynamic attributes use @attributes inside Haml and Slim attribute lists.
- Use
@render,@children, and named slots` to compose templates. - Use
@attributesin a Haml or Slim attribute list to expand dynamic attribute maps.