Go Evaluation
GoHT supports the evaluation of Go code within templates. Haml and Slim use line prefixes for Go code. EGO captures full Go statements between <% ... %> tags and requires valid Go syntax.
Outputting Go
To output the result of Go code in Haml or Slim, you use the equals = character. Following the equals, you may use any valid Go code that evaluates to a string.
When the generated Go code is run, it will produce the following HTML.
Like plain text content, you can include the Go code on the same line after a tag.
When the generated Go code is run, it will produce the same HTML as before.
String values only
You are limited to outputting string values in GoHT. This is a limitation borne out of the need for speed. GoHT is designed to be fast, and to parse each value using something such as fmt.Sprintf("%v", value) would slow down rendering, and it might not always output what you wanted.
If you need to output a value that is not a string, you won’t need to type out the fmt.Sprintf function. Instead, GoHT provides a shorthand way to be able to output a non-string value when you need to.
If you use any of the valid formatting codes, such as %d or %02f followed by a space and then the value you wish to output, GoHT will know that you are trying to output a non-string value and will parse it accordingly.
When the generated Go code is run, it will produce the following HTML.
See the Go docs for fmt for more information on formatting codes.
Running Go
To run Go code in Haml or Slim, you use the dash - character. Following the dash, you may use any valid Go code.
When the generated Go code is run, it will produce the following HTML.
No braces required
In the previous section a for loop was used to loop over the words slice. In Haml and Slim, we could have written the same loop without the braces.
When the generated Go code is run, it will produce the same HTML as before.
Braces may still be used in Haml and Slim when you prefer explicit Go syntax.
Long statement wrapping
Haml and Slim Go statements can wrap onto the next line with a trailing backslash or comma.
EGO Go Tags
EGO templates use tags for Go code and output. Code blocks must contain valid Go, including braces for control flow.
<%= value %> writes escaped output. <%! value %> writes unescaped output. Formatting directives can be used before the value, for example <%= %d count %>.