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.

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

gox
<:switch value={{ status }}>

You can also use properties, constants, calls to functions or any other valid expression of Go.

gox
<:switch value={{ user.Role }}>
gox
<: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.

gox
<:case is={{ "loading" }}>
gox
<:case is={{ StatusReady }}>
gox
<:case is={{ service.DefaultState() }}>

The same block can also respond to several values.

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

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

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

gox
<:case is={{ StatusLoading }} fallthrough>
gox
<: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.

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

  • :switch must declare the attribute value.
  • :switch must contain at least one block :case or :default.
  • :switch can only contain blocks :case and :default.
  • All block :case must declare the attribute is.
  • The attribute is accepts one or more valid expressions of Go separated by commas.
  • :default does not accept attributes.
  • There can only be one block :default.
  • :default must always be the last block of :switch.
  • fallthrough can only be used in blocks :case.
  • A block :case that you use fallthrough must be followed by another :case or 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.

gox
<:switch>
    <:case is={{ "ready" }}>
</:switch>

:case without attribute is.

gox
<:switch value={{ status }}>
    <:case>
</:switch>

:switch empty.

gox
<:switch value={{ status }}>

</:switch>

More than one block :default.

gox
<:switch value={{ status }}>
    <:default>
    <:default>
</:switch>

:default before a block :case.

gox
<:switch value={{ status }}>
    <:default>
    <:case is={{ "ready" }}>
</:switch>

Content other than :case or :default.

gox
<:switch value={{ status }}>
    <div />
</:switch>

fallthrough in the last block :case.

gox
<:switch value={{ status }}>
    <:case is={{ StatusReady }} fallthrough>
</:switch>

Use of fallthrough out of a block :case.

gox
<:default fallthrough>

</:default>
gox
<:switch value={{ status }} fallthrough>

</:switch>

Integration with Go

GOX directly uses the semantics of switch of the Go language.

This means that:

  • value accept any valid expression of Go.
  • is accepts one or more valid expressions of Go.
  • The comparison between value and each :case follows the behavior of the switch de Go.
  • fallthrough it 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 :switch where a single expression can produce different results.
  • Use :if when the decision depends on boolean conditions.
  • Reserve fallthrough for cases where sharing several branches improves the clarity of the template.
  • Keep the blocks :case small and easy to read.

Next step

Now that you know how to select different branches within a temperate, continue with Components to learn to reuse and organize interfaces through components.

Components