Syntax and basic usage

The template package enables us to separate presentation from data, using text files and data structures. The template engine defines two delimiters—left and right—for opening and closing actions that represent data evaluation. The default delimiters are {{ and }}, and the template evaluates only what's included within these delimiters, leaving the rest untouched.

Usually the data bound to the template is a structure or a map and it is accessible anywhere in the template with the $ variable. Whether it's a map or a struct, the fields are always accessed in the same way, using the .Field syntax. If the dollar is omitted, the value is referred to the current context, which is $ if it's not in special statements, such as loops. Outside of these exceptions, the {{$.Field}} and {{.Field}} statements are equivalent.

The flow in a template is controlled with a condition statement, {{if}}, and a loop statement, {{range}}, and both terminate with the {{end}} statement. The condition statement also offers the possibility of a chain {{else if}} statement to specify another condition, acting like a switch, and an {{else}} statement, which can be considered the default of a switch. {{else}} can be used with the range statement and it's executed when the argument of the range is nil or has zero length.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.37.196