GoHT Template Basics

GoHT template files are a mixture of Go code and template blocks. Template blocks can use Haml, Slim, or EGO syntax. Outside those blocks, normal Go rules apply.

Template files should start with a package declaration followed by any necessary imports. Following that is any Go code that is needed for the template. Template code is written inside a @haml, @slim, or @ego block.

Template Blocks

A GoHT template block is defined by a template directive followed by the name of the function that will be generated. The block is then opened with a { and closed with a }.

// in a file named `my_template.goht`
@haml MyTemplate() {
	%p Hello, World!  
}

The same file can also contain Slim or EGO templates:

@slim MySlimTemplate() {
	p Hello, World!
}
@ego MyEgoTemplate() {
	<p>Hello, World!</p>
}

To turn the templates into Go functions, run the goht generate command. This generates a Go file named my_template.goht.go with functions named after each template block.

At the same time, GoHT is parsing the template blocks out of the Go code and compiling them into HTML-writing Go code. This means when you call the MyTemplate function to output HTML, it is already pre-parsed and ready to be outputted. No time or CPU cycles are spent parsing template syntax at runtime.

Generated Templates

When a GoHT template is generated, it will be a Go function that takes the same parameters as the template block. The function will return a goht.Template which has a Render(ctx context.Context, io.Writer) method. This method writes the HTML to the io.Writer.

The original @goht directive is still accepted as a deprecated alias for Haml templates. New documentation and new code should use @haml.