10. Processing data in template with function

It is possible to process data in template when it is displayed. Syntax of the template function is "function(parameter):keyOfData". Part of "(parameter)" can be omitted.

10.1. Built-in functions

ZOGAN provide functions below.

Table 1. ZOGAN Built-in functions

namedesc.
date date() function. The parameter is format string.
sprintf sprintf() function. The parameter is format string.
not provides `NOT` logical operator. No parameter.
bool casts data to boolean value. No parameter.

Now only date and printf have been implemented. The format string is the same of date() and printf() function.

<body>
  <h1>date</h1>
  <p var:cont="{date(F j, Y, g:i a):date}"></p>
  <h1>sprintf</h1>
  <p var:cont="{sprintf(I love %s):printf}"></p>
</body>
   

10.2. User defined functions

For user defined functions, use addFunction() method.

<table border="1">
  <tr>
    <th>number</th><th>category</th>
  </tr>
  <tr var:cont="{numbers}">
    <td var:cont="{number}"></td><td var:cont="{category:number}"></td>
  </tr>
</table>
   

function getCategory($num, $param, $option)
{
    if ($num < 100) {
        return 'Small';
    }
    elseif ($num < 200) {
        return 'Medium';
    }
    else  {
        return 'Large';
    }
}

$tmpl = new zogan;
// snip
$tmpl->addFunction('category', 'getCategory');
$nums = array();
for ($i =0; $i < 10 ;$i++ ) {
    $nums[] = array('number' => mt_rand(0,300));
}
$nums = array('numbers' => $nums);
$tmpl->display($nums);