Template Languages

GoHT templates live in .goht files and can use Haml, Slim, or EGO syntax. You can mix all three in the same file when that is useful.

Haml

Use @haml for Haml-style templates.

@haml Card(title string) {
	.card
		%h2= title
		%p Haml uses percent-prefixed tag names.
}

Haml-specific syntax includes:

  • !!! doctypes.
  • %tag, .class, and #id elements.
  • Attribute lists such as {href: "/docs"}.
  • Object references such as %article[item].
  • Unescaped text with ! and !=.
  • Rendered comments with / and silent comments with -#.
  • Self-closing tags such as %img/.
  • Inline interpolation with #{value}.
  • Go statements with - code and rendered Go values with = code.
  • Filters: :plain, :escaped, :preserve, :javascript, and :css.
  • Long statement wrapping with trailing \ or ,.
  • Whitespace removal markers with > and <.

Slim

Use @slim for Slim-style templates.

@slim Card(title string) {
	.card
		h2= title
		p Slim uses bare tag names.
}

Slim-specific syntax includes:

  • doctype for HTML5 doctypes.
  • Bare tag names such as section, plus .class and #id implicit divs.
  • Attribute lists such as {href: "/docs"}.
  • Inline tags such as li: a.First First Item.
  • Unescaped text with |.
  • Rendered comments with / and preserved HTML comments with /!.
  • Self-closing tags such as img/.
  • Inline interpolation with #{value}.
  • Go statements with - code and rendered Go values with = code or == code.
  • Filters: :javascript and :css.
  • Long statement wrapping with trailing \ or ,.
  • Whitespace addition markers with > and <.

EGO

Use @ego for HTML-oriented templates with Go tags.

@ego Card(title string) {
	<div class="card">
		<h2><%= title %></h2>
		<p>EGO keeps HTML visible and uses tags for Go code.</p>
	</div>
}

EGO supports:

  • <% ... %> for Go code blocks.
  • <%- ... %> for Go code blocks with leading whitespace stripping.
  • <%= ... %> for escaped output. Formatting directives such as <%= %d count %> are supported.
  • <%! ... %> for unescaped output. Formatting directives such as <%! %v trustedValue %> are supported.
  • <%@render ... %>, <%@children %>, and <%@slot ... %> for GoHT composition.
  • <%# ... %> for comments.

EGO supports these closing tags:

  • %> closes a tag normally.
  • -%> closes a tag and strips surrounding whitespace.
  • $%> closes a tag and strips one following newline.

EGO captures full Go statements between tags, so control flow must be valid Go:

@ego List(items []string) {
	<ul>
		<% for _, item := range items { %>
			<li><%= item %></li>
		<% } %>
	</ul>
}

Deprecated @goht

The original @goht directive is still supported for Haml templates, but it is deprecated. Use @haml in new code and keep @goht only when documenting or maintaining backward compatibility.