BMW Z4 GT3 Fuji 1/24 Scale Build

By Frank Apa October 7, 2021

A variable is accessed by referencing the variable name. nfiguration (for sitewide values), or through the meta data of each specific piece of content. You can define any values of any type (supported by your front matter/config format) and use them however you want to inside of your templates.

BMW Z4 GT3 Fuji 1/24 Scale Build

A unique characteristic of go templates is they are content aware. Variables and content will be sanitized depending on the context of where they are used. More details can be found in the go docs.

BMW Z4 GT3

Basic Syntax

BMW Z4 GT3
Go lang templates are html files with the addition of variables and functions.
BMW Z4 GT3

Functions

BMW Z4 GT3

Go template ship with a few functions which provide basic functionality. The go template system also provides a mechanism for applications to extend the available functions with their own. Hugo template functions provide some additional functionality we believe are useful for 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:

{{ add 1 2 }}
BMW Z4 GT3

Functions

Go template ship with a few functions which provide basic functionality. The go template system also provides a mechanism for applications to extend the available functions with their own. Hugo template functions provide some additional functionality we believe are useful for 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:

{{ add 1 2 }}
BMW Z4 GT3

Functions

Go template ship with a few functions which provide basic functionality. The go template system also provides a mechanism for applications to extend the available functions with their own. Hugo template functions provide some additional functionality we believe are useful for 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:

BMW Z4 GT3

Go variables and functions are accessible within {{ }}

Accessing a predefined variable “foo”:

{{ foo }}

Parameters are separated using spaces

Calling the add function with input of 1, 2:

BMW Z4 GT3
{{ add 1 2 }}

Methods and fields are accessed via dot notation

Accessing the Page Parameter “bar”

BMW Z4 GT3
{{ .Params.bar }}

Parentheses can be used to group items together

{{ if or (isset .Params "alt") (isset .Params "caption") }} Caption {{ end }}

Variables

Each go template has a struct (object) made available to it. In hugo each template is passed either a page or a node struct depending on which type of page you are rendering. More details are available on the variables page.

BMW Z4 GT3

A variable is accessed by referencing the variable name. nfiguration (for sitewide values), or through the meta data of each specific piece of content. You can define any values of any type (supported by your front matter/config format) and use them however you want to inside of your templates.

Using Content (page) Parameters

In each piece of content you can provide variables to be used by the templates. This happens in the front matter.

An example of this is used in this documentation site. Most of the pages benefit from having the table of contents provided. Sometimes the TOC just doesn’t make a lot of sense. We’ve defined a variable in our front matter of some pages to turn off the TOC from being displayed.

Here is the example front matter:

---
title: "Permalinks"
date: "2013-11-18"
aliases:
  - "/doc/permalinks/"
groups: ["extras"]
groups_weight: 30
notoc: true
---

Here is the corresponding code inside of the template:

  {{ if not .Params.notoc }}
    <div id="toc" class="well col-md-4 col-sm-6">
    {{ .TableOfContents }}
    </div>
  {{ end }}

Using Site (config) Parameters

In your top-level configuration file (eg, config.yaml) you can define site parameters, which are values which will be available to you in chrome.

For instance, you might declare:

params:
  CopyrightHTML: "Copyright &#xA9; 2013 John Doe. All Rights Reserved."
  TwitterUser: "spf13"
  SidebarRecentLimit: 5

Within a footer layout, you might then declare a <footer> which is only provided if the CopyrightHTML parameter is provided, and if it is given, you would declare it to be HTML-safe, so that the HTML entity is not escaped again. This would let you easily update just your top-level config file each January 1st, instead of hunting through your templates.

{{if .Site.Params.CopyrightHTML}}<footer>
<div class="text-center">{{.Site.Params.CopyrightHTML | safeHtml}}</div>
</footer>{{end}}

An alternative way of writing the “if” and then referencing the same value is to use “with” instead. With rebinds the context . within its scope, and skips the block if the variable is absent:

{{with .Site.Params.TwitterUser}}<span class="twitter">
<a href="https://twitter.com/{{.}}" rel="author">
<img src="/images/twitter.png" width="48" height="48" title="Twitter: {{.}}"
 alt="Twitter"></a>
</span>{{end}}

Finally, if you want to pull “magic constants” out of your layouts, you can do so, such as in this example:

<nav class="recent">
  <h1>Recent Posts</h1>
  <ul>{{range first .Site.Params.SidebarRecentLimit .Site.Recent}}
    <li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
  {{end}}</ul>
</nav>
comments powered by Disqus