Template code Chunk output
{#example_1}
{!--
  -- Starting in 2.1, you don't need to box your objects in
  -- DataCapsule's - you can just bind them directly into the template!
  --
  -- If all you want from the object are its non-private member
  -- fields, you can now bind it to a tag variable directly with set().
  --
  -- CamelCase fieldnames are converted to lowercase_with_underscores.
  --
  -- Primitives (int, long, double, float, etc.) are converted to Strings.
  --
  -- Boolean values are the string "TRUE" if true, or null/undefined
  -- when false, to facilitate {.if ($x.is_something) } branching.
  --
  -- Arrays and Lists can be looped over with {.loop in $obj.some_list}
  --
  -- Other types of fields (besides String) are treated as nested POJOs,
  -- so you can reference a child object's properties from the template
  -- as you would expect:  {$obj.child.child_property}
  --
  -- Object methods are *not* callable from the template, in keeping
  -- with the read-only nature of Chunk templates.
  --}

<ul>
 <li> {$obj.name}</li>
 <li> {$obj.rank}</li>
 <li> {$obj.serial}</li>
 <li> {.if ($obj.is_active) }Active duty{.else}Retired{/if}</li>
 <li> Superior: {$obj.boss.name}</li>
 <li> Pi to two places is {$obj.pi|sprintf(%.2f)}</li>
</ul>

{#}
Theme theme = new Theme("examples");

// Fetch template from this file: themes/examples/pojo.chtml
// Inside that file there is a template "snippet" named #example_1
Chunk html = theme.makeChunk("contrib#example_1");

Thing pojo = new Thing("Bob Roberts","Major","AB231323");

// Bob is his own boss. Circular references don't tie up the interpreter.
pojo.setBoss(pojo);

html.set("obj", pojo);

html.render( out );

//...

static class Thing
{
    String name, rank, serial;
    boolean isActive;
    double pi = Math.PI;
    Thing boss;

    Thing(String name, String rank, String serial)
    {
        this.name = name;
        this.rank = rank;
        this.serial = serial;
        this.isActive = true;
    }

    public void setBoss(Thing boss)
    {
        this.boss = boss;
    }
}

<ul>
 <li> Bob Roberts</li>
 <li> Major</li>
 <li> AB231323</li>
 <li> Active duty</li>
 <li> Superior: Bob Roberts</li>
 <li> Pi to two places is 3.14</li>
</ul>


  • Bob Roberts
  • Major
  • AB231323
  • Active duty
  • Superior: Bob Roberts
  • Pi to two places is 3.14
Template code Chunk output
{#example_2}
{!--
  -- Similar template-binding is available for java beans.
  --
  -- Just use chunk.setToBean("tag", myBean) instead of chunk.set(x,y)
  --
  -- Standard accessors like getFullName() are exposed in the template
  -- as {$tag.full_name} - camelCase converts to lower_with_underscores
  --
  -- Boolean properties with accessors like isActive() appear in the
  -- template as {$tag.is_active} but only when true, w/value "TRUE"
  --
  -- Primitives are converted to Strings.
  --
  -- Arrays and Lists are converted to LIST types that can be looped in.
  --
  -- Nested objects are assumed to be beans as well.
  --}

<ul>
 <li> {$bean.name}</li>
 <li> {$bean.rank}</li>
 <li> {$bean.serial}</li>
 <li> {.if ($bean.is_active) }Active duty{.else}Retired{/if}</li>
 <li> Superior: {$bean.boss.name}</li>
 <li> Pi to two places is {$bean.pi|sprintf(%.2f)}</li>
</ul>

{#}
Theme theme = new Theme("examples");

// Fetch template from this file: themes/examples/pojo.chtml
// Inside that file there is a template "snippet" named #example_1
Chunk html = theme.makeChunk("contrib#example_1");

ThingBean bean = new ThingBean();
bean.setName("Bob Roberts");
bean.setRank("Major");
bean.setSerial("AB231323");
bean.setActive(true);

// Bob is his own boss. Circular references don't tie up the interpreter.
bean.setBoss(bean);

html.setToBean("bean", bean);

html.render( out );

//...

static class ThingBean implements java.io.Serializable
{
    private String name, rank, serial;
    private boolean isActive;
    private double pi = Math.PI;
    private ThingBean boss;

    public ThingBean() {}

    public String getName()
    {
        return this.name;
    }

    public String getRank()
    {
        return this.rank;
    }

    public String getSerial()
    {
        return this.serial;
    }

    public boolean isActive()
    {
        return this.isActive;
    }

    public double getPi()
    {
        return this.pi;
    }

    public ThingBean getBoss()
    {
        return this.boss;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setRank(String rank)
    {
        this.rank = rank;
    }

    public void setSerial(String serial)
    {
        this.serial = serial;
    }

    public void setActive(boolean isActive)
    {
        this.isActive = isActive;
    }

    public void setPi(double pi)
    {
        this.pi = pi;
    }

    public void setBoss(ThingBean boss)
    {
        this.boss = boss;
    }

}

<ul>
 <li> Bob Roberts</li>
 <li> Major</li>
 <li> AB231323</li>
 <li> Active duty</li>
 <li> Superior: Bob Roberts</li>
 <li> Pi to two places is 3.14</li>
</ul>


  • Bob Roberts
  • Major
  • AB231323
  • Active duty
  • Superior: Bob Roberts
  • Pi to two places is 3.14