Conditionals
The conditionals allow to decide which content will be part of the final result of a temperate.
GOX provides four blocks to build conditional structures:
:if:if-group:else-if:else
The blocks :else-if and :else can only be used within a :if-group.
The block :if
The block :if It renders its content only when the condition is true.
<:if condition={{ loggedIn }}>
<h1>Welcome</h1>
</:if> The attribute condition you should always receive a expression valid of Go whose result is a boolean value.
More complex expressions can also be used.
<:if condition={{ user.Age >= 18 && user.Active }}>
<h1>Welcome</h1>
</:if> The block :if-group
When a condition has multiple branches, GOX provides the block :if-group.
<:if-group>
<:if condition={{ loading }}>
Loading...
</:if>
<:else-if condition={{ err != nil }}>
Error
</:else-if>
<:else>
Ready
</:else>
</:if-group> :if-group It groups all the blocks that belong to the same conditional structure.
Note
The conditions described in this chapter are evaluated during the rendering of the server.
When a conditional is part of a Client Component, also participates in the generation of SSRT, allowing to produce a more accurate initial representation before hydration.
After hydration, the SSRT stops intervening and any interface update becomes controlled by the Client Component itself.
See the documentation for Client Components for more information on the operation of the SSRT and the life cycle of a Client Component.
Rules of procedure :if-group
One :if-group must follow a strict structure.
The rules are:
- It must contain exactly one block.
:if. - The block
:ifHe must always be the first. - May contain zero or more blocks
:else-if. - All blocks
:else-ifmust appear after the block:if. - It can contain at least one block.
:else. - If there is a block
:elseIt must always be the last. - The blocks
:else-ifand:elsecan only be used within a:if-group.
The following structure is valid:
if
else-if
else-if
else The following variants are also valid:
if if
else if
else-if if
else-if
else While the following structures are invalid:
else if
else
else-if if
else
else if
if Allocation in :if and :else-if
Both blocks allow a short statement before assessing the condition:
<:if user ok :assign={{ findUser(id) }} condition={{ ok }}>
<p>{{ user.Name }}</p>
</:if> Equivalent to if user, ok := findUser(id); ok { ... } in Go. Bindings are space-separated identifiers and must appear before :assign. At least one other identifier is required _. The expression must produce the amount of values required by the Go declaration; it is also allowed, for example, user ok :assign={{ users[id] }} for a map.
<:if-group>
<:if user ok :assign={{ findUser(id) }} condition={{ ok }}>
<p>{{ user.Name }}</p>
</:if>
<:else-if admin found :assign={{ findAdmin(user) }} condition={{ found }}>
<p>{{ admin.Name }}</p>
</:else-if>
<:else>
<p>No se encontró un administrador para {{ user.Name }}.</p>
</:else>
</:if-group> The allocation is evaluated only once when reaching its branch, before the condition. The variables are visible in its condition, its body and all the subsequent branches of the same group. They are not visible in previous branches or after the group. A posterior branch can declare the same name: its expression of allocation uses the anterior range and the new variable hides it from the condition onwards. It is the range of the following. if ...; ... else if ... de Go.
:else does not support bindings, :assign or other attributes. The writing of the middle branch remains :else-if.
When should you use each one?
Use :if where only one condition exists.
<:if condition={{ loggedIn }}>
Welcome
</:if> Use :if-group where several mutually exclusive branches are to be assessed.
<:if-group>
...
</:if-group> Invalid conditionals
The blocks :else-if and :else cannot be used outside of a :if-group.
For example:
<:else>
</:else> It's invalid.
Similarly:
<:else-if condition={{ condition }}>
</:else-if> He's also invalid.
The attribute condition always use an expression.
For example:
condition=loggedIn It's invalid.
It should be written:
condition={{ loggedIn }} Integration with Go
GOX does not incorporate a language of its own to write conditions.
The conditions directly use the Go language expression system.
This allows you to use variables, operators and calls to functions within the attribute condition without giving up language.
Best practices
As a general recommendation:
- Use
:ifwhere only one condition exists. - Use
:if-groupwhere several mutually exclusive branches are to be assessed. - Keep conditional expressions simple and easy to read.
- It does complex logic before rendering when possible.