🦌

Hugo Translator

a cheat sheet

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 Languages 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 cheat sheat 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

<?php 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

printf is your friend.

From

$fullname = $firstname . ' ' . $lastname

To

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

add joins two strings together but it is limited to two.

delimit brings easy readability and an unlimited number of strings. But the common delimiter may bring issues on more complex sentences. Oh and yeah, it’s a hack.

Conditions

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

bool (the simple one)

fromto
{% if firstname %}
{{ if $firstname  }}

!bool

fromto
{% if !firstname %}
{{ if not $firstname  }}

==

fromto
{% if firstname == "Fabien" %}
{{ if eq $firstname "Fabien" }}

!=

fromto
{% if firstname != "Fabien" %}
{{ if ne $firstname "Fabien" }}

>

fromto
{% if value > 3 %}
{{ if gt $value 3 }}

>=

fromto
{% if value >= 3 %}
{{ if ge $value 3 }}

<

fromto
{% if value > 3 %}
{{ if lt $value 3 }}

<=

fromto
{% if value >= 3 %}
{{ 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 ?

Yep, since Hugo .27 cond lets you do just that1.

from

<?php echo $hour < 12 ? "Good Morning" : "Good Afternoon" ?>

to

{{ cond (lt $hour 12) "Good Morning" "Good Afternoon" }}

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

implode:

<?php 
$names = ["John", "Paul", "Ringo"];
echo implode(", ", $anmes);

to

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

Finding a string in a chain:

if(sentence.indexOf(thatWord) !== -1)
<?php if(strpos($sentence, $thatWord) !== false) ?>

to

{{ if strings.Contains $sentence $thatWord }}
Final notes:

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


  1. Cameron Moore was kind enough to point to the cond function which is in place since 0.27↩︎

Related posts

Comments

comments powered by Disqus