Loops
The loops allow you to repeat content within a template.
GOX provides two blocks to repeat content:
:for, to go through a collection.:repeat, to repeat a block a certain number of times.
The block :for
The block :for allows to travel a collection using the same semantic as the instruction range of the Go language.
<:for index user range={{ users }}>
<p>{{ index }} - {{ user.Name }}</p>
</:for> The attribute range you should always receive a expression valid of Go whose result is a type compatible with the instruction range.
The identifiers declared before the attribute range correspond to the variables produced by the instruction range From Go. Their names are completely free and have no special meaning for GOX.
None, one or two variables can be declared.
<:for range={{ users }}>
...
</:for> <:for value range={{ users }}>
...
</:for> <:for index value range={{ users }}>
...
</:for> The blocks :empty and :each
When it is necessary to distinguish between an empty collection and a collection with elements, :for provides the blocks :empty and :each.
<:for index user range={{ users }}>
<:empty>
No users found.
</:empty>
<:each>
<p>{{ user.Name }}</p>
</:each>
</:for> If the collection is empty only the block is run :empty.
If the collection contains one or more elements only the block is run :each.
Rules of procedure :empty and :each
When one of these blocks is used, they both become mandatory.
The rules are:
:emptyand:eachcan only be used within:for.- If it exists
:emptyIt must also exist.:each. - If it exists
:eachIt must also exist.:empty. - There can only be one block
:empty. - There can only be one block
:each. :emptyshould always appear before:each.- When these blocks are used, there can be no content directly within the
:for.
The following structure is valid:
for
empty
each The blocks are also valid :for not used :empty or :each.
While the following structures are invalid:
for
each
empty for
empty for
each empty each The block :repeat
The block :repeat allows to repeat a block of content a certain number of times.
<:repeat times={{ 5 }}>
Hello World
</:repeat> An optional variable can be declared to access the index of each iteration.
<:repeat index times={{ 5 }}>
{{ index }}
</:repeat> The attribute times you should always receive a expression valid from Go whose result is a type value int.
For example:
<:repeat times={{ 5 }}>
...
</:repeat> <:repeat times={{ count }}>
...
</:repeat> <:repeat times={{ len(users) }}>
...
</:repeat> <:repeat times={{ service.MaxRetries() }}>
...
</:repeat> The blocks :break and :continue
GOX provides the blocks :break and :continue to control the execution of a loop.
<:continue /> <:break /> These blocks can only be used within:
:for:each:repeat
Your behavior is equivalent to the instructions break and continue of the Go language.
Invalid loops
The blocks :empty and :each cannot be used outside a block :for.
For example:
<:empty>
</:empty> It's invalid.
Similarly:
<:each>
</:each> He's also invalid.
The blocks :break and :continue Nor can they be used outside a loop.
<:break /> <:continue /> It is also invalid to declare more than two variables in a block :for.
<:for a b c range={{ users }}> Integration with Go
GOX does not incorporate a system of its own to travel collections.
The block :for directly uses the semantics of the instruction range of the Go language.
Similarly, :repeat use a Go expression to determine the number of iterations.
This allows you to use variables, call to functions and any other valid expression of Go without leaving the language.
Best practices
As a general recommendation:
- Use
:forto go through collections. - Use
:repeatwhen you only need to repeat content a certain number of times. - Use
:emptyto represent the state of an empty collection. - Keep the iteration logic simple and perform any complex processing before rendering.