Quick Reference
Template files
A .goht file can contain package/import declarations, ordinary Go helpers, and one or more templates. Each template starts with a language directive and a Go function declaration:
Use @haml, @slim, or @ego followed by a valid Go function declaration to create a GoHT template. Template names become generated Go functions, so they must be valid Go function names. @goht is a deprecated alias for @haml; migrate old declarations by changing @goht to @haml.
Haml, Slim, and EGO templates can coexist in the same .goht file. See Template Files for the full declaration model.
Language support matrix
| Feature | Haml | Slim | EGO |
|---|---|---|---|
| Template directive | @haml | @slim | @ego |
| Element syntax | %tag, .class, #id | tag, .class, #id | Normal HTML |
| Doctype | !!! | doctype | Normal HTML <!DOCTYPE html> |
| Attributes | {name: value} | {name: value} | Normal HTML attributes |
| Go code | - code, = code | - code, = code, == code | <% code %>, <%= value %>, <%! value %> |
| Comments | /, -# | /, /! | <%# comment %> |
| Filters | :plain, :escaped, :preserve, :javascript, :css | :javascript, :css | Normal HTML elements |
| Object references | Yes | No | No |
| Inline tags | No | li: a.First First Item | Normal HTML nesting |
| Whitespace controls | Removal with > and < | Addition with > and < | Tag controls <%-, -%>, $%> |
Significant Whitespace
Haml and Slim templates accept tabs for indentation. Each template must also be indented with at least one tab. EGO templates require the initial template indentation, but the HTML inside that indentation can use normal HTML spacing.
Doctypes
Only HTML5 is supported by GoHT for Haml and Slim templates. EGO templates write normal HTML, including <!DOCTYPE html> when you need a document doctype. Declare the Haml doctype with:
Declare the Slim doctype with:
Plain text
GoHT uses non-alphanumeric characters to signify that an element, or Go code exists on the line. You can write your plain text content just as it is.
HTML can also be added, and it will make it into the final output unchanged.
Comments
Add rendered HTML comments to Haml or Slim templates with:
Haml comments can be used to comment out a line or lines that you do NOT want rendered into the final output:
EGO comments use <%# comment %> and are removed from the output.
Declaring an element
Haml elements use percent-prefixed tag names. Slim elements use bare tag names. Both languages support implicit div elements with an ID or class:
All other elements can be created using a tag:
These can all be combined, and classes can be repeated:
Tags, ids, and classes are limited to alphanumeric characters, underscores, hyphens, and colons. More complex ids and classes will need to be set on the element as an attribute.
Slim also supports inline tags:
Element attributes
Attributes are added to an element using a pair of curly braces:
Attribute names and their value use a colon between them, and attributes are separated with a comma.
Attribute names are limited to alphanumeric characters, underscores, and hyphens. Complex attributes names must be enclosed in double quotes:
Attribute names cannot be interpolated. Rendered attribute names and values are HTML-escaped. Attribute values can be interpolated and use a string Go variable or expression:
HTML elements also support boolean attributes. In GoHT there are two ways to work with boolean attributes:
We can simply add the attribute name and leave off the value portion. We can also use an interpolated boolean variable or expression. We use a question mark instead of a colon to signal that GoHT should expect a boolean from the interpolated value.
The class attribute has special handling and can parse several sources for classes. See the Attributes :: Classes section for more information.
Dynamic lists of attributes can also be added in Haml and Slim attribute lists:
See Attributes :: Dynamic Attributes for the details on how to use this feature.
Go evaluation
In Haml and Slim, lines of Go can be used to either generate output:
Or can be used to declare variables, add loops, or conditional branching:
GoHT will fill in curly braces for you in most Haml and Slim control-flow lines. Braces may still be used when you prefer explicit Go syntax.
Long Haml and Slim Go statements can wrap onto the next line with a trailing backslash or comma.
EGO captures full Go statements between tags and requires valid Go syntax:
Interpolation
We can include Go variables and expressions in plain text, filters, and attributes. The syntax for interpolation is:
Except a boolean attribute value, all interpolated values must be Go strings. You can quickly format the value into a string using the Go fmt patterns:
See Formatting for more information about formatting Go values and expressions.
Filters
A filter is a phrase prefixed with a colon. It controls how all nested content will be interpreted and rendered.
| Filter | Haml | Slim | EGO |
|---|---|---|---|
:plain | Yes | No | Use normal HTML/text |
:escaped | Yes | No | Use escaped output <%= value %> |
:preserve | Yes | No | Use normal HTML/text |
:javascript | Yes | Yes | Use <script> |
:css | Yes | Yes | Use <style> |
Whitespace removal
GoHT will keep the newlines from the original Haml template. Haml can remove whitespace by focusing on whitespace outside or inside an element:
Slim uses > and < as whitespace addition controls instead of removal controls. EGO uses tag-level controls such as <%-, -%>, and $%>.
Object references
In Haml templates, special Go values can be used to automatically apply an id or class, or both to an element by adding to the tag with square brackets:
See Object References for information on how to use this feature with your Go values.
Nesting templates
GoHT provides @render, @children, and @slot to compose templates. @attributes is an attribute-list directive for Haml and Slim, not a composition directive.
Named slots let a caller pass content to a named region:
Slots can declare default content:
The nested template can also receive content from the parent template:
EGO templates use command tags for composition:
See Nesting Templates for Haml, Slim, EGO, @children, and named slot examples.