Loops and Conditionals
Loops
foreach
When presented with an array variable that contains many objects, you can access the individual values with a foreach loop.
{foreach $[variable]}
<!-- In each iteration of the loop, the $[variable] is an individual object -->
Value this loop: $[variable]
{/foreach}
Example Usage
$[board] object variable, used in a list context
$[thread] object variable, used in a list context
{foreach $[board]}
Board Name: $[board.name]<br />
{/foreach}
will become
Board Name: First Board<br />
Board Name: Second Board<br />
Board Name: Third Board<br />
...
Conditionals
In some cases variables return a value of 0 or 1. The intent of these variables is to be able to utilize them in conditional statements, making certain areas display or hide depending on the value.
if
{if $[variable]}
If the variable is true, show this segment
{/if}
You can also use the inverse, or negated value.
{if !$[variable]}
If the variable is false, show this segment
{/if}
else
{if $[variable]}
If the variable is true, show this segment
{else}
otherwise, show this segment
{/if}
elseif
{if $[variable]}
If this variable is true, show this segment
{elseif $[different_variable]}
or if this variable is true, show this segment
{else}
otherwise, show this segment
{/if}
Example Usage
Conditionals can be very useful when developing your templates.
A common use case is displaying some text only to users who have not yet created a member account:
{if !$[current_user.is_member]}
Hello guest! Please register!
{/if}