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:

@haml MyHamlTemplate() {
	%p Hello from Haml.
}
@slim MySlimTemplate() {
	p Hello from Slim.
}
@ego MyEgoTemplate() {
	<p>Hello from EGO.</p>
}

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

FeatureHamlSlimEGO
Template directive@haml@slim@ego
Element syntax%tag, .class, #idtag, .class, #idNormal HTML
Doctype!!!doctypeNormal 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, :cssNormal HTML elements
Object referencesYesNoNo
Inline tagsNoli: a.First First ItemNormal HTML nesting
Whitespace controlsRemoval 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.

@haml MyTemplate() {
	%html
		%head
			%title= "Hello, World!"
		%body
			%h1= "Hello, World!"
}

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:

doctype

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 rendered HTML comments to Haml or Slim 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.

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:

#some-id
.some-class

All other elements can be created using a tag:

%span
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.

Slim also supports inline tags:

li: a.First First Item

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 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:

%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 in Haml and Slim attribute lists:

%span{@attributes: #{attrs}}

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:

= 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 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:

<% for _, term := range terms { %>
	<span><%= term %></span>
<% } %>

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.

FilterHamlSlimEGO
:plainYesNoUse normal HTML/text
:escapedYesNoUse escaped output <%= value %>
:preserveYesNoUse normal HTML/text
:javascriptYesYesUse <script>
:cssYesYesUse <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:

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

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:

%span[someValue]

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.

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

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

Named slots let a caller pass content to a named region:

@haml Layout() {
	%main
		=@slot main
}
err := Layout().Render(ctx, w, PageBody().Slot("main"))

Slots can declare default content:

@haml Layout() {
	%main
		=@slot main
			%p Default content
}

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

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

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

EGO templates use command tags for composition:

@ego ParentTemplateEgo() {
	<%@render ChildTemplate() { %>
		<span>Content from the parent</span>
	<% } %>
}

See Nesting Templates for Haml, Slim, EGO, @children, and named slot examples.