Control Structures

Simple variable replacement is not enough for more complex, personalized templates. Sailthru supports several types of control structures, conditionals, and loops:

{if}

Usage: {if expression}result{/if}

You can use {if}…{/if} to display something only based on a particular condition. For if's purposes, 0, "" (empty string), null, and false all evaluate to false, and anything else evaluates to true.

{if name}
<p>Your name is {name}!</p>
{/if}

{if}...{else}...{/if}

Usage: {if expression1}result 1{else if expression2}result 2{else}result 3{/if}

You can add any number of {else} expressions to an {if}.

In this example, result 1 will be the result if expression1' evaluates to true, result 2 will be the result if expression1 evaluates to false but expression2 evaluates to true, and result 3 will be the result if both expression1 and expression2' evaluate to false.

Ternary Operator ? :

Usage: {expression1 ? expression2 : expression3}

Will return expression2 if expression1 evaluates to true, otherwise will return expression3.

Not technically a control structure, but you can use the ternary operator as a handy inline shortcut for if in many situations.

<p>Dear {gender == 'M' ? 'Mr.' : 'Ms.'} {last_name},</p>

Alternate usage: expression1 ?: expression2

In abbreviated form, it will return expression1 if expression1 evaluates to true; otherwise it will return expression2. This is especially useful if you don't know if a variable is set or not:

<p>Your current status is: {status ?: 'Unknown'}</p>

{switch}

Usage: {switch expression}{case expression}case{/case}{case expression}case{/case}…{/switch}

Given a series of {case}s, picks the first one that matches the value.

{switch gender}
{case 'M'}You are a man!{/case}
{case 'F'}You are a woman!{/case}
{/switch}

{select}

Usage: {select}{case expression}case{/case}{case expression}case{/case}…{/select}

Given a series of {case}s, evaluates them all and picks the one with the highest numeric value. If there is a tie, the tie will go to the earlier {case}.

This is especially useful when combined with Horizon profiling functions like horizon_interest() – you can provide different content based on which of several interests is the strongest.

{select}
{case horizon_interest('menswear,fashion')}Check out our new suits!{/case}
{case horizon_interest('purses')}Try out our new purses{/case}
{case horizon_interest('jewelry')}Look at our jewelry selection!{/case}
{/select}

{foreach}

Usage: {foreach expression as variable} loop contents {/foreach}

Loop through a list or object, assigning a temporary variable to each item.

<div>Your order is as follows:</div>
<table>
{foreach order as o}
<tr>
<td>{o.qty}</td>
<td>{o.name}</td>
</tr>
{/foreach}
</table>
key-value {foreach}

Usage: {foreach expression as keyvar, valuevar} loop contents {/foreach}

You can also loop through both keys and values. If you loop through a list, the key will be the integer index of the item (starting at 0).

<ul>
{foreach content as i, c}
<li>Item #{i+1}: {c.url}</li>
{/foreach}
</ul>

{* comments *}

Enclose Zephyr comments in {* braces with asterisks *}. Unlike HTML comments, Zephyr comments will not render and are not visible to end users.

{* start the main header block here *}
{include 'header'}

{* now loop through the content *}
{foreach content as c}
{* fill this in later... *}
{/foreach}
 
/var/www/docs.sailthru.com/htdocs/data/pages/syntax/control-structures.txt · Last modified: 2011/10/10 16:23 by Ian White
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki