Template code | Chunk output | |
---|---|---|
{#example_1} {!-- -- Chunk's conditional expressions are useful but somewhat -- limited. The lightweight parser can only handle these -- simple string value tests listed below. -- -- Sorry, {.if} conditions may not include greater-than -- or less-than tests (eg > < <= gt lt etc). Also, Chunk's -- conditional expressions do not support the && and || -- (and/or) operators. However, the inclusion of regular -- expression pattern matching should cover most of your -- exotic comparison needs. Anything more involved is best -- handled in your business logic anyway, and not in your -- presentation layer. -- -- The following tests will function as expected: --} <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> {!-- 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 {/if} 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 {/if} {.elseIf ($color == blue) } {.if ($radical) } Green {.else} Liberal {/if} {.else} Centrist {/if} 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. |