| Template code | Chunk output | |
|---|---|---|
{!-- Supply a default from the template, to output when
-- tag is not defined. Everything after the colon :
-- becomes the tag value (NB: null and undefined are
-- NOT the same - see example 4 below).
--}
<p>Hello {$name:there}! You're our grand prize winner!</p>
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();
|
|
<p>Hello there! You're our grand prize winner!</p> Hello there! You're our grand prize winner! |
| Template code | Chunk output | |
|---|---|---|
{!-- Default to the empty string, no tag preservation. --}
<div>{$error_message:}</div>
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></div> |
| Template code | Chunk output | |
|---|---|---|
{!-- The |onempty() filter covers cases where you don't
-- need to differentiate between undefined and defined-but-empty.
--
-- onempty outputs its argument when the tag is:
-- (A) only whitespace, or
-- (B) is the empty string, or
-- (C) is not defined at all.
--
-- The pipe character | marks the start of a tag output filter.
--}
Hello {$name|onempty(there)}! You're our grand prize winner!
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();
|
|
Hello there! You're our grand prize winner!
Hello there! You're our grand prize winner!
|
| Template code | Chunk output | |
|---|---|---|
{!-- IMPORTANT!! The following two calls are equivalent:
--
-- c.set("tag", null);
-- c.set("tag", "");
--
-- Chunk.set() does null-trapping -- turns null into the empty string.
-- Furthermore, the empty string is considered "defined" when
-- rendering the template.
--
-- So, if you *want* a default value to display (eg {$tag:default} ),
-- don't use null!! Instead, make sure a tag is undefined like so:
--
-- // call setOrDelete with an expression that evaluates to null
-- x = null;
-- c.setOrDelete("tag", x); // setOrDelete does no null-trapping
-- // or...
-- c.unset("tag");
-- // or just don't call .set() at all!
-- // in the example below, "tag" will be undefined except on Wed.
-- if (isWednesday) {
-- c.set("tag", "hump day");
-- }
--}
<div>{$tag:default}</div>
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>default</div> default
|