Quick Reference

GoHT templates

A GoHT template is started with the following command:

@goht MyTemplate() {

}

Use @goht followed by a valid Go function declaration to create a GoHT template.

Significant Whitespace

Use either tabs or spaces to indent lines. Lines that are indented further to the right than the line above it are considered to be “nested” under the line above.

Doctypes

Only HTML5 is supported by GoHT. Declare the doctype for a document 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.

Write your content just how you would like to see it.

HTML can also be added, and it will make it into the final output unchanged.

HTML can be added as plain text too.
<span>This span element will not be altered and is plain text too</span>

Comments

Add HTML comments to your GoHT templates with:

/ This is a HTML comment and it will be rendered into the final output.

Haml comments can be used to comment out a line or lines that you do NOT want rendered into the final output:

-# This is a Haml comment and it will NOT be rendered into the final output.

Declaring an element

Quickly create a div element with an ID or class:

#some-id
.some-class

All other elements can be created using a tag:

%span

These can all be combined, and classes can be repeated:

%span#some-id.some-class.another-class

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.

Element attributes

Attributes are added to an element using a pair of curly braces:

%span{id: "some-id", class: "some-class another-class"}

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:

%span{"x:bind":"data"}

Attribute values can also be interpolated and use a string Go variable or expression:

%span{id: #{spanID}}

HTML elements also support boolean attributes. In GoHT there are two ways to work with boolean attributes:

%button{disabled}
%button{disabled? #{isDisabled}}

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:

%span{@attributes: #{attrs}}

See Attributes :: Dynamic Attributes for the details on how to use this feature.

Go evaluation

Lines of Go can be used to either generate output:

= sayGreeting("World")
%span= sayGreeting("Again")

Or can be used to declare variables, add loops, or conditional branching:

- terms := []string["shoe", "foot", "glove", "hand"]
- for _, term := range terms
  %span= term

GoHT will fill in curly braces for you in most cases. In the above example, we were able to leave off the opening and closing curly braces.

Interpolation

We can include Go variables and expressions in plain text, filters, and attributes. The syntax for interpolation is:

#{someValue}

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:

#{%t someBoolean}

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. Only the following filters are supported by GoHT:

  • :javascript - Renders the content into a <script> element
  • :css - Renders the content into a <style> element
  • :plain - Renders all children, even lines that appear to be GoHT/Haml as plain text.
  • :escaped - Renders all content with HTML escaped replacements of problematic characters.
  • :preserve - Renders the content with the newlines preserved in the output.

Whitespace removal

GoHT will keep the newlines from the original template. You can remove these and any other whitespace by focusing on whitespace outside or inside an element:

%span> Eliminate whitespace outside the element.
%span< Eliminate whitespace inside the element.
%span<> Eliminate all whitespace.

Object references

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:

%span[someValue]

See Object References for information on how to use this feature with your Go values.

Nesting Templates

GoHT provides two commands that can be used to nest a template inside another template:

@goht ChildTemplate() {
%span Child template content
}

@goht ParentTemplate() {
%span Parent template content
=@render ChildTemplate()
}

The nested template can also receive content from the parent template:

@goht ChildTemplate() {
%span Child template content
%span.parent-content=@children
}

@goht ParentTemplate() {
%span Parent template content
=@render ChildTemplate()
  %span Content from the parent
}