--- title: Templating linkTitle: Templating description: Hugo uses Go's `html/template` and `text/template` libraries as the basis for the templating. categories: [templates,fundamentals] keywords: [go] menu: docs: parent: templates weight: 20 weight: 20 toc: true aliases: [/layouts/introduction/,/layout/introduction/, /templates/go-templates/] --- {{% note %}} The following is only a primer on Go Templates. For an in-depth look into Go Templates, check the official [Go docs](https://golang.org/pkg/text/template/). {{% /note %}} Go Templates provide an extremely simple template language that adheres to the belief that only the most basic of logic belongs in the template or view layer. ## Basic syntax Go Templates are HTML files with the addition of [variables][variables] and [functions][functions]. Go Template variables and functions are accessible within `{{ }}`. ### Access a predefined variable A _predefined variable_ could be a variable already existing in the current scope (like the `.Title` example in the [Variables](#variables) section below) or a custom variable (like the `$address` example in that same section). ```go-html-template {{ .Title }} {{ $address }} ``` Parameters for functions are separated using spaces. The general syntax is: ```go-html-template {{ FUNCTION ARG1 ARG2 .. }} ``` The following example calls the `add` function with inputs of `1` and `2`: ```go-html-template {{ add 1 2 }} ``` #### Methods and fields are accessed via dot notation Accessing the Page Parameter `bar` defined in a piece of content's [front matter]. ```go-html-template {{ .Params.bar }} ``` #### Parentheses can be used to group items together ```go-html-template {{ if or (isset .Params "alt") (isset .Params "caption") }} Caption {{ end }} ``` #### A single statement can be split over multiple lines ```go-html-template {{ if or (isset .Params "alt") (isset .Params "caption") }} ``` #### Raw string literals can include newlines ```go-html-template {{ $msg := `Line one. Line two.` }} ``` ## Variables Each Go Template gets a data object. In Hugo, each template is passed a `Page`. In the below example, `.Title` is one of the elements accessible in that [`Page` variable][pagevars]. With the `Page` being the default scope of a template, the `Title` element in current scope (`.` -- "the **dot**") is accessible simply by the dot-prefix (`.Title`): ```go-html-template {{ .Title }} ``` Values can also be stored in custom variables and referenced later: {{% note %}} The custom variables need to be prefixed with `$`. {{% /note %}} ```go-html-template {{ $address := "123 Main St." }} {{ $address }} ``` Variables can be re-defined using the `=` operator. The example below prints "Var is Hugo Home" on the home page, and "Var is Hugo Page" on all other pages: ```go-html-template {{ $var := "Hugo Page" }} {{ if .IsHome }} {{ $var = "Hugo Home" }} {{ end }} Var is {{ $var }} ``` Variable names must conform to Go's naming rules for [identifiers][identifier]. ## Functions Go Templates only ship with a few basic functions but also provide a mechanism for applications to extend the original set. [Hugo template functions][functions] provide additional functionality specific to building websites. Functions are called by using their name followed by the required parameters separated by spaces. Template functions cannot be added without recompiling Hugo. ### Example 1: adding numbers ```go-html-template {{ add 1 2 }} ``` ### Example 2: comparing numbers ```go-html-template {{ lt 1 2 }} ``` Note that both examples make use of Go Template's [math][math] functions. {{% note %}} There are more boolean operators than those listed in the Hugo docs in the [Go Template documentation](https://golang.org/pkg/text/template/#hdr-Functions). {{% /note %}} ## Includes When including another template, you will need to pass it the data that it would need to access. {{% note %}} To pass along the current context, please remember to include a trailing **dot**. {{% /note %}} The templates location will always be starting at the `layouts/` directory within Hugo. ### Partial The [`partial`][partials] function is used to include _partial_ templates using the syntax `{{ partial "/." . }}`. Example of including a `layouts/partials/header.html` partial: ```go-html-template {{ partial "header.html" . }} ``` ### Template The `template` function was used to include _partial_ templates in much older Hugo versions. Now it's useful only for calling [_internal_ templates][internal templates]. The syntax is `{{ template "_internal/