--- title: Introduction to Hugo Templating linktitle: Introduction description: Hugo uses Go's `html/template` and `text/template` libraries as the basis for the templating. godocref: https://golang.org/pkg/html/template/ date: 2017-02-01 publishdate: 2017-02-01 lastmod: 2017-02-25 categories: [templates,fundamentals] keywords: [go] menu: docs: parent: "templates" weight: 10 weight: 10 sections_weight: 10 draft: false aliases: [/layouts/introduction/,/layout/introduction/, /templates/go-templates/] toc: true --- {{% note %}} The following is only a primer on Go Templates. For an in-depth look into Go Templates, check the official [Go docs](http://golang.org/pkg/html/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. {{< youtube gnJbPO-GFIw >}} ## 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]({{< relref "#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: ``` {{ 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 }} ``` ## Variables {#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 }} ``` {{% warning %}} For Hugo v0.47 and older versions, variables defined inside `if` conditionals and similar are not visible on the outside. See [https://github.com/golang/go/issues/10608](https://github.com/golang/go/issues/10608). Hugo has created a workaround for this issue in [Scratch](/functions/scratch). {{% /warning %}} For **Hugo v0.48** and newer, variables can be re-defined using the new `=` operator (new in Go 1.11). Below example will work only in these newer Hugo versions. The example 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 }} ``` ## 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 functions][]. {{% note "Additional Boolean Operators" %}} There are more boolean operators than those listed in the Hugo docs in the [Go Template documentation](http://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 useful only for calling [*internal* templates][internal_templates]. The syntax is `{{ template "_internal/