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 template value, such as *HelloTemplate above, that implements the goht.Template interface:
Every generated template type also has a WithChildren method for the built-in children slot, plus a With<Slot> method for each @slot it declares. See Render and Children and Slots for details.
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, @slot, @ifslot, @noslot, and @eachslot. Dynamic attributes use @attributes inside Haml and Slim attribute lists.
- Use
@renderand@childrento compose templates. - Use named slots for layouts with several independent content regions.
- Use
@attributesin a Haml or Slim attribute list to expand dynamic attribute maps.