Template code | Chunk output | |
---|---|---|
{#example_1} {!-- -- SYNTAX -- -- If statements come in two flavors, compact and standard: -- -- compact syntax: {.if (cond) }...{/if} nice for one-liners. -- standard syntax: {% if (cond) %}...{% endif %} easier to read. -- -- From 3.1.0 on, outer parens are now optional: -- {% if $x == $y %} ... {% endif %} -- -- Quoting static strings with no whitespace is optional. -- {% if ($x == "bob") %} is the same as {% if ($x == bob) %} -- -- Chunk's conditional expressions are useful but somewhat -- limited. The lightweight parser can only handle the -- simple string value tests listed below. -- string equality: == -- string inequality: != -- regex match: =~ -- regex negative match: !~ -- -- Starting in 3.3.0, conditional expressions may be -- grouped with parens and joined using || and && operators. -- -- LIMITATIONS -- -- Tag values are coerced to strings for template output in an -- earlier step. This means that when if-expressions are -- evaluated for true/false, all value comparisons are -- string comparisons *only*, never numeric equivalence -- nor any notion of java-style equality. -- -- Booleans map to the string "TRUE" or null/undefined, so -- they will be interpreted as expected in a simple if-test -- for existence: -- {% if ($x.is_active) } ... {% endif %} -- -- String equality/inequality can handle either a static string -- or a $tag expression on the right hand side of the operator. -- However, the left side *must* be a tag expression and may -- not be a simple string. -- "Yoda" style IS NOT OK: {% if (bob == $x|lower) %} -- OK: {% if ($x|lower == bob) %} -- OK: {% if ($x|lower == $y|lower) %} -- -- "if" comparisons do not include greater-than or less-than -- tests (eg: > < <= gt lt etc) but there is a workaround. -- -- Starting in 3.1.1, numeric comparison is possible via the -- comp filter. -- {% if $n|comp(< 0) %} ... {% endif %} --} <ol> {!-- existence --} <li>{.if ($flag) }Flag is defined{/if}</li> <li>{.if (!$flag) }Flag is not defined{/if}</li> {!-- equality / inequality --} <li>{.if ($tag == apples) }Tag was apples{/if}</li> <li>{.if ($tag != apples) }Tag was not apples{/if}</li> <li>{.if ($tag == "apples") }Tag was "apples"{/if}</li> <li>{.if ($tag != "apples") }Tag was not "apples"{/if}</li> <li>{.if ($tag_a == $tag_b) }Tag A equals Tag B{/if}</li> <li>{.if ($tag_a != $tag_b) }Tag A does not equal Tag B{/if}</li> {!-- perl-style pattern matching --} <li>{.if ($tag =~ /apples|oranges/) }Tag is apples or oranges{/if}</li> <li>{.if ($tag !~ /apples|oranges/) }Tag is neither apples nor oranges{/if}</li> </ol> {#} Theme theme = new Theme("examples"); // Fetch template from this file: themes/examples/ifelse.chtml // Inside that file there is a template "snippet" named #example_1 Chunk html = theme.makeChunk("ifelse#example_1"); html.set("flag", "TRUE"); html.set("tag", "apples"); html.set("tag_a", "apples"); html.set("tag_b", "oranges"); buffer.append( "FIRST RUN:\n" ); buffer.append( html.toString() ); // The same Chunk can be re-used with different tag values html.resetTags(); // note: not setting a value for "flag" this time html.set("tag", "bananas"); html.set("tag_a", "kumquat"); html.set("tag_b", "kumquat"); buffer.append( "SECOND RUN:\n" ); buffer.append( html.toString() ); |
|
FIRST RUN: <ol> <li>Flag is defined</li> <li></li> <li>Tag was apples</li> <li></li> <li>Tag was "apples"</li> <li></li> <li></li> <li>Tag A does not equal Tag B</li> <li>Tag is apples or oranges</li> <li></li> </ol> SECOND RUN: <ol> <li></li> <li>Flag is not defined</li> <li></li> <li>Tag was not apples</li> <li></li> <li>Tag was not "apples"</li> <li>Tag A equals Tag B</li> <li></li> <li></li> <li>Tag is neither apples nor oranges</li> </ol>
FIRST RUN:
|
Template code | Chunk output | |
---|---|---|
{#example_2} <p>{$state} is a {% if ($color == red) %} Conservative {% elseIf ($color == blue) %} Liberal {% else %} Centrist {% endif %} state.</p> {#} Theme theme = new Theme("examples"); // Fetch template from this file: themes/examples/ifelse.chtml // Inside that file there is a template "snippet" named #example_2 Chunk html = theme.makeChunk("ifelse#example_2"); html.set("state", "Texas"); html.set("color", "red"); html.render( out ); |
|
<p>Texas is a Conservative state.</p> Texas is a Conservative state. |
Template code | Chunk output | |
---|---|---|
{#example_3} {!-- Nest your conditionals, no problem. --} <p>{$state} is a {% if ($color == red) %} {% if ($radical) %} Tea Party {% else %} Conservative {% endif %} {% elseIf ($color == blue) %} {% if ($radical) %} Green {% else %} Liberal {% endif %} {% else %} Centrist {% endif %} state.</p> {#} Theme theme = new Theme("examples"); // Fetch template from this file: themes/examples/ifelse.chtml // Inside that file there is a template "snippet" named #example_3 Chunk html = theme.makeChunk("ifelse#example_3"); html.set("state", "Vermont"); html.set("color", "blue"); html.set("radical", "TRUE"); html.render( out ); |
|
<p>Vermont is a Green state.</p> Vermont is a Green state. |
Template code | Chunk output | |
---|---|---|
{#example_4} {!-- From 3.3.0 on, compound logical expressions are permitted --} {% if ($can_edit && $can_admin) || ($name == Harry) %} <button>All-Powerful Admin Button</button> {% endif %} {#} Theme theme = new Theme("examples"); // Fetch template from this file: themes/examples/ifelse.chtml // Inside that file there is a template "snippet" named #example_4 Chunk html = theme.makeChunk("ifelse#example_4"); html.set("name", "Harry"); html.render( out ); |
|
<button>All-Powerful Admin Button</button> |