Switch
The block :switch allows you to select a single branch of the Template based on the result of a Go expression. It is the recommended alternative when the same expression can take different possible values. When only boolean conditions should be evaluated, use :if.
<:switch value={{ status }}>
<:case is={{ "loading" }}>
<Spinner />
</:case>
<:case is={{ "ready" }}>
<Dashboard />
</:case>
<:default>
<ErrorMessage />
</:default>
</:switch> :switch
The block :switch evaluates the expression indicated in the attribute value and compare it to each of the blocks :case contents inside.
The attribute value is mandatory and accepts any valid expression of Go whose result can be compared by a switch.
<:switch value={{ status }}> You can also use properties, constants, calls to functions or any other valid expression of Go.
<:switch value={{ user.Role }}> <:switch value={{ service.State() }}> :case
The blocks :case represent the different branches that you can select a :switch.
Each block :case must declare the attribute is, which accepts one or more valid expressions of Go separated by commas.
<:case is={{ "loading" }}> <:case is={{ StatusReady }}> <:case is={{ service.DefaultState() }}> The same block can also respond to several values.
<:case is={{ StatusReady, StatusCached }}> The blocks :case are evaluated in the order in which they appear. When one of them coincides with the value produced by valuethe subsequent blocks are no longer evaluated, unless the current block declares the attribute fallthrough.
fallthrough
The attribute fallthrough allows to continue immediately with the next branch of the :switch, even when its attribute is Don't match.
Their behaviour is equivalent to that of fallthrough in Go.
<:switch value={{ status }}>
<:case is={{ StatusLoading }} fallthrough>
<Spinner />
</:case>
<:case is={{ StatusPreparing }}>
<ProgressBar />
</:case>
</:switch> In the above example, when the value of status be StatusLoadingThey'll be so relegated. <Spinner /> how <ProgressBar />.
fallthrough can also continue to a block :default.
<:switch value={{ status }}>
<:case is={{ StatusLoading }} fallthrough>
<Spinner />
</:case>
<:default>
<UnknownStatus />
</:default>
</:switch> fallthrough It is a Boolean attribute. Its position with respect to the other attributes does not change the behavior of the block.
Both examples are equivalent.
<:case is={{ StatusLoading }} fallthrough> <:case fallthrough is={{ StatusLoading }}> :default
The block :default defines the branch that will be executed when no block :case is consistent with the value assessed by :switch.
Its use is optional.
<:switch value={{ status }}>
<:case is={{ StatusReady }}>
<Dashboard />
</:case>
<:default>
<ErrorMessage />
</:default>
</:switch> Rules of procedure :switch
For a block :switch is valid, the following rules must be met:
:switchmust declare the attributevalue.:switchmust contain at least one block:caseor:default.:switchcan only contain blocks:caseand:default.- All block
:casemust declare the attributeis. - The attribute
isaccepts one or more valid expressions of Go separated by commas. :defaultdoes not accept attributes.- There can only be one block
:default. :defaultmust always be the last block of:switch.fallthroughcan only be used in blocks:case.- A block
:casethat you usefallthroughmust be followed by another:caseor a block:default. - The order of the attributes does not change the behavior of the block.
Invalid switches
The following examples produce a compilation error.
:switch without attribute value.
<:switch>
<:case is={{ "ready" }}>
</:switch> :case without attribute is.
<:switch value={{ status }}>
<:case>
</:switch> :switch empty.
<:switch value={{ status }}>
</:switch> More than one block :default.
<:switch value={{ status }}>
<:default>
<:default>
</:switch> :default before a block :case.
<:switch value={{ status }}>
<:default>
<:case is={{ "ready" }}>
</:switch> Content other than :case or :default.
<:switch value={{ status }}>
<div />
</:switch> fallthrough in the last block :case.
<:switch value={{ status }}>
<:case is={{ StatusReady }} fallthrough>
</:switch> Use of fallthrough out of a block :case.
<:default fallthrough>
</:default> <:switch value={{ status }} fallthrough>
</:switch> Integration with Go
GOX directly uses the semantics of switch of the Go language.
This means that:
valueaccept any valid expression of Go.isaccepts one or more valid expressions of Go.- The comparison between
valueand each:casefollows the behavior of theswitchde Go. fallthroughit conserves exactly the same semantic as in Go.
To understand how GOX evaluates the expressions used by value e is, consult the documentation of the Expressions.
Best practices
- Use
:switchwhere a single expression can produce different results. - Use :if when the decision depends on boolean conditions.
- Reserve
fallthroughfor cases where sharing several branches improves the clarity of the template. - Keep the blocks
:casesmall and easy to read.