Var Tag

The var tag allows you to include a variable in a template. There are several advantages over using the {BRACES} syntax, like the ability to set a default value or a modifier.

<patTemplate:tmpl name="foo">
  Here is a variable:
  <patTemplate:var name="myvar" default="my default value"/>



The name of the variable.


A default value for the variable if it is not defined.




The copyFrom attribute allows the designer to add variables to a template and copy the value from any other variable.


May be one of the following values:

  • htmlentities
  • nl2br
  • wordwrapper
  • dateformat
  • strtoupper
  • strlen
  • strrev
  • HTML_Img
  • Expression
  • Translate (Joomla only)




This attribute is used in conjunction with the dateformat modifier.

Used with the HTML_Img modifier.

If you check for more complicated expressions than 'equals', you may use the expression modifier. It allows you to specify any expression plus two return values, one for true and one for false.

A logical expression. You may use $self to indicate the actual value of the variable.

The value to replace into the variable if the expression evaluates to true.

The value to replace into the variable if the expression evaluates to false.


Convert html characters.

<patTemplate:tmpl name="page">
Apply a modifier to sometext:
<patTemplate:var name="sometext" modifier="htmlentities" modifierType="php"/>

Apply a modifier to multiline:

<patTemplate:var name="multiline" modifier="nl2br" modifierType="php"/>

Apply a modifier to a long text:

<patTemplate:var name="wrap" modifier="wordwrapper"
    modifierType="custom" width="10" cut="no" nl2br="yes"/>


<patTemplate:var name="wrap2" modifier="wordwrapper"
    modifierType="custom" width="20" cut="yes" nl2br="yes"/>

Dateformat modfifier:

<patTemplate:var name="birthday" modifier="dateformat" format="%d.%m.%Y"/>

Examples using copyFrom:

<patTemplate:tmpl name="page">
<table border="1">
        <td><patTemplate:var name="sometext_pretty"
                modifier="nl2br" copyfrom="sometext"/></td>
<patTemplate:tmpl name="nested">
        <td><patTemplate:var name="sometext" copyfrom="page.sometext"

Copying a variable that's not scalar:

<patTemplate:tmpl name="repeating">
    <br />
<patTemplate:tmpl name="non-repeating">
    <patTemplate:var name="newVar" copyFrom="repeating.value"/>
    <br />

Example of an expression modifier:

<patTemplate:tmpl name="page">
The variable: {VAR}<br />
Is it bigger than 13?
    We are using &gt; instead of >, as this tag is a valid XML tag.
<patTemplate:var name="foo" copyFrom="var" modifier="Expression" <em><strong /></em>
expression="$self&gt;13" true="Yes!" false="Nay!"/>


Translating a Variable (Joomla only)

<mos:var name="text" modifier="Translate" />

Variable Modifiers

Applying modifers is really a piece of cake. All you have to do is to use the <patTemplate:var/> tag instead of only enclosing the variable in { and }. Then you may use the 'modifier' attribute to set the modifier.

<patTemplate:tmpl name="page">
           <patTemplate:var name="myVar" modifier="nl2br"/>

Variable modifiers will be applied to the value of a variable when parsing the template. There two types of modfifiers:

* You may use any PHP function as a variable modifier. The modifier must accept only a string as a parameter and also return a string. Perfect examples for variable modifiers are nl2br() or htmlentities().
* You may create custom modifiers, which provide extended functionality and which may be influenced by the attributes of the var tag.

Last Updated ( Saturday, 15 October 2005 )