Templight

Core functions

There is only 3 functions that are used to map data with a template file.

Simple replacement

This function allow you to replace a var tag by a value :

$tpl->replace('myTag','my value');
<var name="myTag"/>

Iterative replacement

This function allow you to iteratively replace a tag with an associative array. This associative array must have a root array that contain other associative array.

This function is perfect to generate lists, tables, whatever.

$array = array(
	array('href'=>'http://dosimple.ch',text=>'doSimple'),
	array('href'=>'http://google.ch',text=>'Google'),
	array('href'=>'http://ibm.ch',text=>'IBM'));

$tpl->iterativeReplace('myLinks',$array);
<ul>
    <loop name="myLinks">
    <li>
        <a href="<var name="href"/>"><var name="text"/></a>
    </li>
    </loop>
</ul>

Recursive replacement

This function is similar to iterative replace but can handle any level of nested associative array. The associative array must also have a root array that contain other associative array.

This function is usefull to generate nested structures like navigation lists or sitemaps.

$recursiveArray = array(
    array('href'=>'http://dosimple.ch',text=>'doSimple'),
    array('href'=>'http://google.ch',text=>'Google',
        array('href'=>'http://microsoft.ch',text=>''Microsoft'),
        array('href'=>'http://slashdot.ch',text=>''Slashdot'),
        ),
    array('href'=>'http://ibm.ch',text=>'IBM'));

$tpl->recursiveReplace('siteMap',$recursiveArray);
<recursion name="siteMap">
<if test="node.level()==1">
<loop><h2><a href="<var name="link"/>">
<var name="text"/></a></h2><recursion/></loop>
<else/>
<ul>
    <loop>
        <li>
            <a href="<var name="href"/>">
                <var name="text"/>
            </a>
        <recursion/>
        </li>
    </loop>
</ul>
</if>
</recursion>