diff --git a/content/en/templates/introduction.md b/content/en/templates/introduction.md index 0b9ad051b..9614ce40a 100644 --- a/content/en/templates/introduction.md +++ b/content/en/templates/introduction.md @@ -20,26 +20,39 @@ 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/). +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. +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 `{{ }}`. +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 -``` -{{ foo }} +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 following example calls the `add` function with inputs of `1` and `2`: +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 }} ``` @@ -47,30 +60,38 @@ Parameters for functions are separated using spaces. The following example calls 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 {#variables} -Each Go template gets a data object. In Hugo, each template is passed a `Page`. See [variables][] for more information. +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]. -This is how you access a `Page` variable from a template: +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: -``` -{{ $address := "123 Main St."}} +{{% note %}} +The custom variables need to be prefixed with `$`. +{{% /note %}} + +```go-html-template +{{ $address := "123 Main St." }} {{ $address }} ``` @@ -83,28 +104,28 @@ Hugo has created a workaround for this issue in [Scratch](/functions/scratch). ## Functions -Go templates only ship with a few basic functions but also provide a mechanism for applications to extend the original set. +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 }} -=> 3 + ``` ### Example 2: Comparing Numbers -``` +```go-html-template {{ lt 1 2 }} -=> true (i.e., since 1 is less than 2) + ``` -Note that both examples make use of Go template's [math functions][]. +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). +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 @@ -124,116 +145,174 @@ within Hugo. The [`partial`][partials] function is used to include *partial* templates using the syntax `{{ partial "/." . }}`. -Example: +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 is still useful for calling [*internal* -templates][internal_templates]: +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/