GoHT Template Basics

GoHT Template files are a mixture of Go and Haml code. Save for our Haml code, all the same rules for writing Go code 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. The Haml code is then written inside a @goht block.

@goht Blocks

A GoHT template block is defined by the @goht keyword followed by the name of the function that will be generated. The block is then opened with a { and closed with a }. The Haml code is written inside the block.

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

To turn the above Haml code into a Go function, we would run the goht generate command. This would generate a Go file named my_template.goht.go with a function named MyTemplate that would then render HTML.

At the same time, GoHT is parsing the Haml blocks out of the Go code it is also parsing them into HTML. 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 the Haml code at runtime.

@goht Templates

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