This page documents the switch instruction and the other forms of the for instruction.
The switch instruction can be used as an alternative to if when the condition is not simply true or false. It executes a block of code based on the condition value:
{% switch department.Name %}
{% case "Rugs" %}
Rugs and mats on offer for the home
{% case "Pillows", "Pillowcases" %}
Bedroom offers
{% default %}
Various offers
{% end %}
Bedroom offers
The values of each case must have the same type as the condition.
The case values are evaluated in order and only the code of the first one that matches the condition is executed. If no case matches and default is present, the default code is executed.
The condition can be omitted; in that case, each case value must be a boolean expression:
{% switch %}
{% case stock > 10 %}
Available
{% case name == "Pillows" %}
Bedroom offers
{% default %}
Various offers
{% end %}
Bedroom offers
The for instruction repeatedly executes the code inside it. There are five forms of for, but for most template uses it is enough to know for in described separately. The ones documented below are the other forms of for.
The for range form is similar to for in but also assigns the index of the element, starting from zero:
{% for i, product := range products %}
<div>{{ i+1 }}. {{ product.Name }}</div>
{% end %}
If you want the index and element values to be assigned to two previously declared variables, use = instead of :=, as in this example:
{% var i int %}
{% var product Product %}
{% for i, product = range products %}
<div>{{ i+1 }}. {{ product.Name }}</div>
{% end %}
There are {% i %} products and the last one is called {{ product.Name }}
If one of the two variables is not used, you can use _ in its place:
{% for _, product = range products %}
<div>{{ i+1 }}</div>
{% end %}
If only the index is used, you can write:
{% for i := range products %}
<div>{{ i+1 }}</div>
{% end %}
If neither variable is used, you can omit both:
{% for range products %}
<div></div>
{% end %}
The for instruction can be used with a condition. In this case, the code inside for is executed as long as the condition is true.
{% for i < len(products) %}
<div>{{ i+1 }}. {{ products[i].Name }}</div>
{% i++ %}
{% end %}
The condition of the {% for condition %} instruction can be of any type. This is different from the for condition in Go, which can only be boolean.
The for instruction without a condition executes its code until it is interrupted with a break instruction.
{% for %}
{% var v = value() %}
{% if not v %}{% break %}{% end %}
{% end %}
The for init; condition; post form consists of three parts separated by a semicolon ;. The first part declares or assigns a variable before the first iteration, the middle part is the condition, and the last part increments a variable or otherwise assigns it a new value before each iteration after the first.
The iteration continues as long as the condition is true.
{% for i := 0; i < len(products); i++ %}
<div>{{ i+1 }}. {{ products[i].Name }}</div>
{% end %}
The condition of the {% for init; condition; post %} instruction can be of any type. This is different from the for condition in Go, which can only be boolean.
The continue instruction is used inside a for loop to stop the current iteration and resume execution with the next one.
For example, the following code shows only the products in products that have a price:
{% for product in products %}
{% if not product.Price %}
{% continue %}
{% end if %}
<div>{{ product.Name }}</div>
{% end %}
The same could be done without using continue, but using continue can make the code simpler and more readable.
The break instruction is used inside a for loop to stop execution entirely.
For example, the following code outputs 1 2 3:
{% for n in []int{1, 2, 3, 4, 5} %}
{% if n > 3 %}
{% break %}
{% end if %}
{{ n }}
{% end %}