{"body_class":"rp-body rp-section-post rp-full-js","section":"post","thisUrl":"http://regisphilibert.com/blog/2017/04/hugo-go-template-translator-explained-understanding/","title":"Regis Philibert | Hugo Translator","translation_url":null}

Hugo Translator

Go Template is the templating language used by Hugo and other Go systems. Now it is not Twig or Blade or Liquid, don’t get fooled by the familiar curlies. It is a far stretch from those well known Templating Language out there…

{{ printf "Hello %s %s. %s" $firstname (slicestr $middlename 0 1) $lastname }}

What is this 😨

{{ if or (eq firstname 'George') (eq firstname 'Daniel') }}

or that ? 😱

The first time you’re gonna stumble on the syntax above may be a bit deconcerting if you come from more conventional languages.

I spent a lot a time trying to figure out how best to use Go Template.

Using comparison with more familiar syntaxes, I wrote this modest article to try and help unveil the misteries of Go Template

Arguments

Go Template functions don’t use any parentheses to contain arguments, nor commas to seperate such arguments

From

printf("Hello %s %s", $firstname, $lastname);

To

{{ printf "Hello %s %s" $firstname $lastname }}

We can use a function as an argument, that’s when parentheses come handy.

{{ printf "Hello %s %s. %s" $firstname (slicestr $middlename 0 1) $lastname }}

Concatenation

Well printf is your friend, your only friend.

From

$fullname = $firstname . ' ' . $lastname

To

{{ $fullname := printf "%s %s" $firstname $lastname }}

Conditions

Conventional templating languages systematically offer an intuitive syntax for conditions. Go Template doest not. It gives you some bool functions to use in place of conditions.

==

from
{% if firstname == 'Fabien' %}
to
{{ if eq firstname 'Fabien' }}

!=

from
{% if firstname != 'Fabien' %}
to
{{ if ne firstname 'Fabien' }}

>

from
{% if value > 3 %}
to
{{ if gt value 3 }}

>=

from
{% if value >= 3 %}
to
{{ if ge value 3 }}

<

from
{% if value > 3 %}
to
{{ if lt value 3 }}

<=

from
{% if value >= 3 %}
to
{{ if le value 3 }}

What about OR, AND ?

Well… or and and are other functions which take an unlimited number of arguments. We can use the ‘conditionnal’ functions mentionned above inside parentheses as arguments so…

OR

from

{% if firstname == 'George' or firstname == 'Daniel' %}

to

{{ if or (eq firstname 'George') (eq firstname 'Daniel') }}

AND

from

{% if value >= 3 and value <= 6 %}

to

{{ if and (ge value 3) (le value 6) }}

What about ternary operators, x ? x : y ?

Not in Go you don’t :/

Variables

Variables in Hugo’s Go Template can be useful but carries a major caveat. There is no way to simply override a variable value other than using .Scratch as explained here

For clarity I prefixed my variables with the old fashioned $ but you don’t have to.

Declaring a variable, no matter its type :

{{ $city := "Montreal" }}

Strings

Pretty straight forwards, same exemple as above.

Arrays

They’re called slice and are declared as such:

{{ $names := slice "John" "Paul" "Ringo" }}

Associative arrays

Associative arrays are called maps. You can create a flat one with dict. dict takes an even number of arguments where odds are keys and evens values.

{{ $beatle := dict "firstname" "John" "lastname" "Lennon" "birthplace" "Liverpool" }}

To create a mutlilevel associative array you would have to use dict inside dict

{{ $beatles := dict "lead" (dict "firstname" "John" "lastname" "Lennon" "birthplace" "Liverpool" ) "drummer" (dict "firstname" "Ringo" "lastname" "Starr" "birthplace" "Liverpool" )}}

Index / Getting array values

Index is the function you use to retrieve values from slices or maps. The first argument is the first level key/index, the following ones are they keys/index after that level.

Using our exemples from above :

{{ index $names 1 }}
// Paul

{{ index $beatle "lastname" }}
// Lennon

{{ index $beatles "drummer" "lastname" }}
//Starr

Helpers

from

$names = ["John", "Paul", "Ringo"]
echo implode(", ", $anmes);
// Outputs John, Paul, Ringo

to

{{ $names := slice "John" "Paul" "Ringo" }}
{{ delimit $names ", "}}
Final notes:

I’ll keep updating the article with new notions in the mean time you can suggest some in the comments.

comments powered by Disqus