Template code Chunk output
{!-- This example showcases exec macros, "if" conditionals and loops.
  -- The template to execute can be inline, defined in the optional
  -- {% body %}...{% endbody %} section, or you can execute a template
  -- snippet from anywhere in your theme.
  --
  -- {% exec [template-name] %}
  --
  --    {% data [@[xml|json|json-strict]] %}
  --      PARAMS...
  --    {% enddata %}
  --
  --    {% body %}
  --      ...
  --    {% endbody %}
  --
  -- {% endexec %}
  --
  -- If both a template reference *and* a {% body %} section are
  -- provided, the template reference will take priority.
  --
  -- A simple xml parser is built-in, but you must add the json-smart
  -- library jar or jackson-databind to your classpath if you wish to
  -- use "@json" or "@json-strict" parameter formatting.
  --}
{% exec %}

 {% data @json %}
{
  name: "Jim and Della",
  assets: [{name:"Hair",value:"195"},
           {name:"Watch",value:"145"}]
}
 {% enddata %}

 {% body %}
<div>

 <h1>{$name}</h1>

 {% if ($assets) %}
 <h2>Assets</h2>
 <ul>
  {% loop in $assets as $asset %}
  <li>{$asset.name} - ${%  $asset.value|sprintf(%.2f)  %}</li>
  {% endloop %}
 </ul>
 {% endif %}

</div>
 {% endbody %}

{% endexec %}
Theme theme = new Theme();

// fetch template from themes/example.chtml
Chunk html = theme.makeChunk("example");

// Insert business logic here.
// Normally you write a lot of html.set("this","that") calls here.

StreamWriter out = getStreamWriter();
html.render( out );

///// or, return the rendered template as a string
// return html.toString();
<div>

 <h1>Jim and Della</h1>

 <h2>Assets</h2>
 <ul>
  <li>Hair - $195.00</li>
  <li>Watch - $145.00</li>
 </ul>

</div>

Jim and Della

Assets

  • Hair - $195.00
  • Watch - $145.00
Template code Chunk output
{!-- {% include %} is simpler but does not offer a data prep step.
  --
  -- {% exec %}...{% endexec %} runs any template macro-style,
  -- with different data assignments per use.
  --
  -- For convenience, {% data %} and {% body %} tags are optional;
  -- if one section is present, the rest is assumed to be the other.
  --
  -- NB: If {% data %} tags are omitted, data format can be specified
  -- in the exec tag, eg: {% exec @xml %}
  --}
<h2>Include:</h2>
<p class="docnote">Simpler, but no data prep.</p>

{% include #example_3 %}

<hr/>

<h2>Exec:</h2>
<p class="docnote">Provide data for included template.</p>

{% exec #example_3 @json %}
 {name:"Armstrong",
  message:"Your turn to buy beer."}
{% endexec %}
Theme theme = new Theme();

// fetch template from themes/example.chtml
Chunk html = theme.makeChunk("example");

// Insert business logic here.
// Normally you write a lot of html.set("this","that") calls here.

StreamWriter out = getStreamWriter();
html.render( out );

///// or, return the rendered template as a string
// return html.toString();
<h2>Include:</h2>
<p class="docnote">Simpler, but no data prep.</p>

<div>
 Earth to {$name}.  Come in, {$name}.
</div>


<hr/>

<h2>Exec:</h2>
<p class="docnote">Provide data for included template.</p>

<div>
 Earth to Armstrong.  Come in, Armstrong.
  <p>Your turn to buy beer.</p>
</div>

Include:

Simpler, but no data prep.

Earth to {$name}. Come in, {$name}.

Exec:

Provide data for included template.

Earth to Armstrong. Come in, Armstrong.

Your turn to buy beer.

Template code Chunk output
{#example_3}
<div>
 Earth to {$name}.  Come in, {$name}.
 {% if ($message) %}
  <p>{$message}</p>
 {% endif %}
</div>
{#}
//...
<div>
 Earth to {$name}.  Come in, {$name}.
</div>

Earth to {$name}. Come in, {$name}.
Template code Chunk output
{!--
  --    {% exec [template] @xml %}     (tag values passed via XML)
  --}
{% exec #example_3 @xml %}
<values>
 <name>XML</name>
 <message>Oh, XML, you're so bloated.</message>
</values>
{% endexec %}
Theme theme = new Theme();

// fetch template from themes/example.chtml
Chunk html = theme.makeChunk("example");

// Insert business logic here.
// Normally you write a lot of html.set("this","that") calls here.

StreamWriter out = getStreamWriter();
html.render( out );

///// or, return the rendered template as a string
// return html.toString();
<div>
 Earth to XML.  Come in, XML.
  <p>Oh, XML, you're so bloated.</p>
</div>

Earth to XML. Come in, XML.

Oh, XML, you're so bloated.

Template code Chunk output
{!--
  --    {% exec [template] %}    (default arg-passing format)
  --}
{% exec #example_3 %}
 {$name = Apollo}
{% endexec %}
Theme theme = new Theme();

// fetch template from themes/example.chtml
Chunk html = theme.makeChunk("example");

// Insert business logic here.
// Normally you write a lot of html.set("this","that") calls here.

StreamWriter out = getStreamWriter();
html.render( out );

///// or, return the rendered template as a string
// return html.toString();
<div>
 Earth to Apollo.  Come in, Apollo.
</div>

Earth to Apollo. Come in, Apollo.
Template code Chunk output
{!--
  --    {% exec [template] %}
  --
  -- This example shows tag assignments in simple form ($name)
  -- and long form ($message).
  --
  --}
{% exec #example_3 %}
 {$name = Apollo}
 {$message=}{% include lorem_ipsum %}{=}
{% endexec %}
Theme theme = new Theme();

// fetch template from themes/example.chtml
Chunk html = theme.makeChunk("example");

// Insert business logic here.
// Normally you write a lot of html.set("this","that") calls here.

StreamWriter out = getStreamWriter();
html.render( out );

///// or, return the rendered template as a string
// return html.toString();
<div>
 Earth to Apollo.  Come in, Apollo.
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>

Earth to Apollo. Come in, Apollo.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Template code Chunk output
{!--
  -- XML attributes are assigned to an {$elt_name@attr_name} tag.
  -- Nested elements are just a dot away: {$parent_elt.child_elt}
  --}
{% exec %}
  {% data @xml %}
<values>
 <name>XML</name>
 <message type="taunt">Oh, XML, you're so bloated.</message>
 <favorite_place>
   <name>Tahiti</name>
 </favorite_place>
</values>
  {% enddata %}

<div>
  Earth to {$name}.  Come in, {$name}.
  {% if ($message) %}
    <p class="{$message@type}">{$message}</p>
  {% endif %}

  <p>{$name} likes to travel to {$favorite_place.name}.</p>

</div>

{% endexec %}
Theme theme = new Theme();

// fetch template from themes/example.chtml
Chunk html = theme.makeChunk("example");

// Insert business logic here.
// Normally you write a lot of html.set("this","that") calls here.

StreamWriter out = getStreamWriter();
html.render( out );

///// or, return the rendered template as a string
// return html.toString();

<div>
  Earth to XML.  Come in, XML.
    <p class="taunt">Oh, XML, you're so bloated.</p>

  <p>XML likes to travel to Tahiti.</p>

</div>


Earth to XML. Come in, XML.

Oh, XML, you're so bloated.

XML likes to travel to Tahiti.