Display variables

Use of the var tag

Var is the most common tag of the templating language that is used by all core functions. This tag could exists by itself or could be nested in loop or recursion. The var tag is an orphan tag and must contain nothing.


The boring hello world example would be like this :

<var name="myText"/>
$tpl->replace('myText','Hello World');

Use of the regexvar tag

Regexvar tag take a input variable and apply a regular expression on this value. Each time the regular expression match somewhere in the value the match is remplaced by the content of the regexvar tag.

Each time a match is found, a local regex tag is available to display part of the regular expression. The conditions also can test matches by using the node object.


This first example illustrate the use of the regexvar function to extract three values from a date string in YYYYmmdd format. Values are displayed in a human readable way using conditions to get the name of the month in plain text.

We are living the
<regexvar name="myDate" regex="([0-9]{4})([0-9]{2})([0-9]{2})">
<regex index="3"/>th of 
    <if test="node.field(2)==1">January</if>
    <if test="node.field(2)==2">February</if>
    <if test="node.field(2)==3">March</if>
    <if test="node.field(2)==4">April</if>
    <if test="node.field(2)==5">May</if>
    <if test="node.field(2)==6">June</if>
    <if test="node.field(2)==7">July</if>
    <if test="node.field(2)==8">August</if>
    <if test="node.field(2)==9">September</if>
    <if test="node.field(2)==10">October</if>
    <if test="node.field(2)==11">November</if>
    <if test="node.field(2)==12">December</if>
<regex index="1"/>

As you see, it's pretty easy to extract data with regular expression and display it in a complex way.

This second example is a little bit more tricky. It show how to replace new lines by the famous HTML br tag :

<regexvar name="myText" regex="([^\n]*)\n([^\n]*)">
    <regex index="1"/>
        <br />
    <regex index="2"/>