Templight
Conditions
In any block structure of TempLight (loop, recursion, regexvar), conditions could be used to generate custom text.
For that purpose a standard if, elseif, else structure is provided. The tests are performed on a unique object called node that represent the current node of information that is rendered.
This node object contains several functions to retrieve usefull values. The Node class is defined at the end of TempLight file and can easily be extended acording your needs.
A list of available functions
- field(key) : return the value of the field specifiec by key
- level() : return horizontal position of the node in the tree
- parent() : return the parent of the current node
- nbBrother() : return the number of son of the parent of this node
- nbChildren() : return the number of son of this node
- brother(index) : return the brother of the current node given by the index. 0 is the position of the node itself. Negative values can be provided to reach preceding brothers
- isFirst() : return true if the node is the first child of his parent
- isLast() : return true if the node is the last child of his parent
- index() : return the vertical position of the node in this parent
- isMultipleOf(number) : return true if the index of the node is a multiple of the number
- child(index) : return the child of the current node wich has the vertical position equal to index
Of course you are free to add new functions to the Node object. You are even ecouraged to do so.
Example
An example that generate a sitemap with condition :
$nestedNavigation = array(
array("text"=>'Enterprise','link'=>'http://www.dosimple.ch/Enterprise/',
array("text"=>'Sales','link'=>'http://www.dosimple.ch/Sales/'),
array("text"=>'Orders','link'=>'http://www.dosimple.com/Orders/')
),
array("text"=>'Agenda','link'=>'http://www.dell.com/',
array("text"=>'Agenda details','link'=>'http://www.dosimple.ch/'),
array("text"=>'Agenda users','link'=>'http://www.dosimple.ch/',
array("text"=>'Alice','link'=>'http://www.dosimple.ch/','current'=>true),
array("text"=>'Bob','link'=>'http://www.dosimple.ch/'),
array("text"=>'John','link'=>'http://www.dosimple.ch/')
)
),
array("text"=>'Contact','link'=>'http://www.dosimple.ch/',
array("text"=>'Alice','link'=>'http://www.dosimple.ch/'),
array("text"=>'Bob','link'=>'http://www.dosimple.ch/'),
array("text"=>'John','link'=>'http://www.dosimple.ch/')
),
);
$tpl->recursiveReplace('basic',$nestedNavigation);
<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="link"/>">
<var name="text"/>
<if test="node.field(current)==true">
«-- you are here</if>
</a>
<recursion/>
</li>
</loop>
</ul>
</if>
</recursion>