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.

gox
<: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.

gox
<:for range={{ users }}>
    ...
</:for>
gox
<:for value range={{ users }}>
    ...
</:for>
gox
<: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.

gox
<: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:

  • :empty and :each can 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.
  • :empty should always appear before :each.
  • When these blocks are used, there can be no content directly within the :for.

The following structure is valid:

text
for
empty
each

The blocks are also valid :for not used :empty or :each.

While the following structures are invalid:

text
for
  each
  empty
text
for
  empty
text
for
  each
text
empty
text
each

The block :repeat

The block :repeat allows to repeat a block of content a certain number of times.

gox
<:repeat times={{ 5 }}>
    Hello World
</:repeat>

An optional variable can be declared to access the index of each iteration.

gox
<: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:

gox
<:repeat times={{ 5 }}>
    ...
</:repeat>
gox
<:repeat times={{ count }}>
    ...
</:repeat>
gox
<:repeat times={{ len(users) }}>
    ...
</:repeat>
gox
<:repeat times={{ service.MaxRetries() }}>
    ...
</:repeat>

The blocks :break and :continue

GOX provides the blocks :break and :continue to control the execution of a loop.

gox
<:continue />
gox
<: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:

gox
<:empty>

</:empty>

It's invalid.

Similarly:

gox
<:each>

</:each>

He's also invalid.

The blocks :break and :continue Nor can they be used outside a loop.

gox
<:break />
gox
<:continue />

It is also invalid to declare more than two variables in a block :for.

gox
<: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 :for to go through collections.
  • Use :repeat when you only need to repeat content a certain number of times.
  • Use :empty to represent the state of an empty collection.
  • Keep the iteration logic simple and perform any complex processing before rendering.

Next step

It continues with documentation of the Switches.

Switch