<HTML ><HEAD ><TITLE >Variables</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Smarty Manual" HREF="index.html"><LINK REL="UP" TITLE="Smarty For Template Designers" HREF="smarty.for.designers.html"><LINK REL="PREVIOUS" TITLE="Escaping Smarty Parsing" HREF="language.escaping.html"><LINK REL="NEXT" TITLE="Variables loaded from config files" HREF="language.config.variables.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="chapter" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Smarty Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.escaping.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.config.variables.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.variables" ></A >Chapter 4. Variables</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="language.variables.html#language.assigned.variables" >Variables assigned from PHP</A ></DT ><DT ><A HREF="language.config.variables.html" >Variables loaded from config files</A ></DT ><DT ><A HREF="language.variables.smarty.html" >{$smarty} reserved variable</A ></DT ></DL ></DIV ><P > Smarty has several different types of variables. The type of the variable depends on what symbol it is prefixed or enclosed within. </P ><P > Variables in Smarty can be either displayed directly or used as arguments for <A HREF="language.syntax.functions.html" >functions</A >, <A HREF="language.syntax.attributes.html" >attributes</A > and <A HREF="language.modifiers.html" >modifiers</A >, inside conditional expressions, etc. To print a variable, simply enclose it in the <A HREF="variable.left.delimiter.html" >delimiters</A > so that it is the only thing contained between them. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN360" ></A ><P ><B >Example 4-1. Example variables</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{$Name} {$product.part_no} <b>{$product.description}</b> {$Contacts[row].Phone} <body bgcolor="{#bgcolor#}"></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Top Tip: </B >As easy way to examine Smarty variables is with the <A HREF="chapter.debugging.console.html" >debugging console</A >. </P ></BLOCKQUOTE ></DIV > </P ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.assigned.variables" ></A >Variables assigned from PHP</H1 ><P > Variables that are <A HREF="api.assign.html" >assigned</A > from PHP are referenced by preceding them with a dollar sign <TT CLASS="literal" >$</TT > (like php). Variables assigned from within a template with the <A HREF="language.custom.functions.html#language.function.assign" ><TT CLASS="varname" >{assign}</TT ></A > function are also displayed this way. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN374" ></A ><P ><B >Example 4-2. Assigned variables</B ></P ><P > php script</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'firstname'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Doug'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'lastname'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Evans'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'meetingPlace'</span><span style="color: #007700">, </span><span style="color: #DD0000">'New York'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">display</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P > where the content of <TT CLASS="filename" >index.tpl</TT > is: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Hello {$firstname} {$lastname}, glad to see you can make it. <br /> {* this will not work as $variables are case sensitive *} This weeks meeting is in {$meetingplace}. {* this will work *} This weeks meeting is in {$meetingPlace}.</PRE ></TD ></TR ></TABLE ><P > This above would output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Hello Doug Evans, glad to see you can make it. <br /> This weeks meeting is in . This weeks meeting is in New York.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.variables.assoc.arrays" ></A >Associative arrays</H2 ><P > You can also reference associative array variables that are assigned from PHP by specifying the key after the '.' (period) symbol. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN386" ></A ><P ><B >Example 4-3. Accessing associative array variables</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contacts'</span><span style="color: #007700">,<br /> array(</span><span style="color: #DD0000">'fax' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'555-222-9876'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'email' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'zaphod@slartibartfast.example.com'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'phone' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'home' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'555-444-3333'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'cell' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'555-111-1234'</span><span style="color: #007700">)<br /> )<br /> );<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">display</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P > where the content of <TT CLASS="filename" >index.tpl</TT > is: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{$Contacts.fax}<br /> {$Contacts.email}<br /> {* you can print arrays of arrays as well *} {$Contacts.phone.home}<br /> {$Contacts.phone.cell}<br /></PRE ></TD ></TR ></TABLE ><P > this will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >555-222-9876<br /> zaphod@slartibartfast.example.com<br /> 555-444-3333<br /> 555-111-1234<br /></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.variables.array.indexes" ></A >Array indexes</H2 ><P > You can reference arrays by their index, much like native PHP syntax. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN397" ></A ><P ><B >Example 4-4. Accessing arrays by index</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contacts'</span><span style="color: #007700">, array(<br /> </span><span style="color: #DD0000">'555-222-9876'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'zaphod@slartibartfast.example.com'</span><span style="color: #007700">,<br /> array(</span><span style="color: #DD0000">'555-444-3333'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'555-111-1234'</span><span style="color: #007700">)<br /> ));<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">display</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P > where <TT CLASS="filename" >index.tpl</TT > is: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{$Contacts[0]}<br /> {$Contacts[1]}<br /> {* you can print arrays of arrays as well *} {$Contacts[2][0]}<br /> {$Contacts[2][1]}<br /></PRE ></TD ></TR ></TABLE ><P > This will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >555-222-9876<br /> zaphod@slartibartfast.example.com<br /> 555-444-3333<br /> 555-111-1234<br /></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.variables.objects" ></A >Objects</H2 ><P > Properties of <A HREF="advanced.features.html#advanced.features.objects" >objects</A > assigned from PHP can be referenced by specifying the property name after the <TT CLASS="literal" >-></TT > symbol. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN410" ></A ><P ><B >Example 4-5. Accessing object properties</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >name: {$person->name}<br /> email: {$person->email}<br /></PRE ></TD ></TR ></TABLE ><P > this will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >name: Zaphod Beeblebrox<br /> email: zaphod@slartibartfast.example.com<br /></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.escaping.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.config.variables.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Escaping Smarty Parsing</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="smarty.for.designers.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Variables loaded from config files</TD ></TR ></TABLE ></DIV ></BODY ></HTML >