| Template code | Chunk output | |
|---|---|---|
{!-- Some handy sprintf examples, in case your sprintf is rusty.
-- Number casting to longs/floats is automatic.
--}
{$x:3.555559|sprintf(%.3f)} Three decimal places (float)<br/>
{$x:3|sprintf(%05d)} Zero-pad five places (integer)<br/>
{$x:3|sprintf(%-5d)} Left-justify, field-length 5 (int)<br/>
{$x:3000000|sprintf(%,d)} Add thousands separators (integer)<br/>
{$x:-3.99|sprintf(%d)} {$y:3.99|sprintf(%d)} Truncate to integer<br/>
{$x:-3.5|sprintf(%.0f)} {$y:3.5|sprintf(%.0f)} Round to integer<br/>
{$x:0.000031415|sprintf(%.3e)} scientific notation<br/>
{$x:255|sprintf(%x)} lowercase hexadecimal<br/>
{$x:255|sprintf(%X)} uppercase hexadecimal<br/>
{$x:255|sprintf(%o)} octal<br/>
"{$x:65|sprintf(%c)}" ascii character<br/>
{$x:banana|sprintf(Throw the %s)}! Text may come before the format expr...<br/>
{$x:banana|sprintf(Throw the %s!)} ...but never after.<br/>
{$x:0xFF|sprintf(%d)} hex in, decimal out<br/>
0x{$x:128|sprintf(%X)} decimal in, hex out<br/>
{$x:#00FF00|sprintf(#%06x)} HEX in (rgb #triple), hex out<br/>
<p>%N.Ms min/max size of %s string output (truncate/pad to fit)</p>
<pre>
{$x:Swiss Army Knife|sprintf(%10.10s)} truncate
{$x:Cloak|sprintf(%-10.10s)} pad to fit, left-justify
{$x:Dagger|sprintf(%10.10s)} pad to fit, standard right-justify
</pre>
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();
|
|
3.556 Three decimal places (float)<br/>
00003 Zero-pad five places (integer)<br/>
3 Left-justify, field-length 5 (int)<br/>
3,000,000 Add thousands separators (integer)<br/>
-3 3 Truncate to integer<br/>
-4 4 Round to integer<br/>
3.141e-05 scientific notation<br/>
ff lowercase hexadecimal<br/>
FF uppercase hexadecimal<br/>
377 octal<br/>
"A" ascii character<br/>
Throw the banana! Text may come before the format expr...<br/>
[Unknown format !: "Throw the %s!",banana] ...but never after.<br/>
255 hex in, decimal out<br/>
0x80 decimal in, hex out<br/>
#00ff00 HEX in (rgb #triple), hex out<br/>
<p>%N.Ms min/max size of %s string output (truncate/pad to fit)</p>
<pre>
Swiss Army truncate
Cloak pad to fit, left-justify
Dagger pad to fit, standard right-justify
</pre>
3.556 Three decimal places (float)
00003 Zero-pad five places (integer) 3 Left-justify, field-length 5 (int) 3,000,000 Add thousands separators (integer) -3 3 Truncate to integer -4 4 Round to integer 3.141e-05 scientific notation ff lowercase hexadecimal FF uppercase hexadecimal 377 octal "A" ascii character Throw the banana! Text may come before the format expr... [Unknown format !: "Throw the %s!",banana] ...but never after. 255 hex in, decimal out 0x80 decimal in, hex out #00ff00 HEX in (rgb #triple), hex out %N.Ms min/max size of %s string output (truncate/pad to fit)
Swiss Army truncate
Cloak pad to fit, left-justify
Dagger pad to fit, standard right-justify
|