| 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"/> </patTemplate:tmpl> AttributesnameThe name of the variable. defaultA default value for the variable if it is not defined. hidden?? copyFromThe copyFrom attribute allows the designer to add variables to a template and copy the value from any other variable. modifierMay be one of the following values:
?? modifierTypeformat 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. ExamplesConvert 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 > instead of >, as this tag is a valid XML tag. --> <patTemplate:var name="foo" copyFrom="var" modifier="Expression" <em><strong /></em> expression="$self>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>
* 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(). |
|
| Last Updated ( Saturday, 15 October 2005 ) |