Documentation Menu

1.5 Template Project

The Joomla! Documentation Working Group is running a project to develop detailed reference and tutorial material on Joomla! 1.5 templates.  There is a project page on the documentation wiki where you can see the work in progress and help us by contributing your knowledge.

Who's Online

We have 16 guests online

Help Site License

The Joomla! Help Site content is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution NonCommercial ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Home arrow patTemplate arrow patTemplate Tags arrow Var Tag

Var Tag PDF Print E-mail

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"/>
</patTemplate:tmpl>

Attributes

name

The name of the variable.

default

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

hidden

??

copyFrom

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

modifier

May be one of the following values:

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

??

modifierType

format

This attribute is used in conjunction with the dateformat modifier.
width
cut
nl2br
title

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.
expression

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

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

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

Examples

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"/>

Again:

<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">
    <tr>
        <th>template</th>
        <th>var</th>
        <th>value</th>
    </tr>
    <tr>
        <td>page</td>
        <td>SOMETEXT</td>
        <td>{SOMETEXT}</td>
    </tr>
    <tr>
        <td>page</td>
        <td>SOMETEXT_PRETTY</td>
        <td><patTemplate:var name="sometext_pretty"
                modifier="nl2br" copyfrom="sometext"/></td>
    </tr>
<patTemplate:tmpl name="nested">
    <tr>
        <td>nested</td>
        <td>SOMETEXT</td>
        <td><patTemplate:var name="sometext" copyfrom="page.sometext"
                modifier="strrev"/></td>
    </tr>
</patTemplate:tmpl>
</table>

Copying a variable that's not scalar:

<patTemplate:tmpl name="repeating">
    {PAT_ROW_VAR} : {VALUE}
    <br />
</patTemplate:tmpl>
<patTemplate:tmpl name="non-repeating">
    {SCALAR} {PAT_ROW_VAR} :
    <patTemplate:var name="newVar" copyFrom="repeating.value"/>
    <br />
</patTemplate:tmpl>

Example of an expression modifier:

<patTemplate:tmpl name="page">
<div>
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!"/>
</div>
</patTemplate:tmpl>

 

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">
      <div>
           <patTemplate:var name="myVar" modifier="nl2br"/>
      </div>
</patTemplate:tmpl>


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 )
 
< Prev   Next >