<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 4. Variables</title> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Smarty Manual"> <link rel="up" href="smarty.for.designers.html" title="Part II. Smarty For Template Designers"> <link rel="prev" href="language.escaping.html" title="Escaping Smarty Parsing"> <link rel="next" href="language.config.variables.html" title="Variables loaded from config files"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Chapter 4. Variables</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="language.escaping.html">Prev</a> </td> <th width="60%" align="center">Part II. Smarty For Template Designers</th> <td width="20%" align="right"> <a accesskey="n" href="language.config.variables.html">Next</a> </td> </tr> </table> <hr> </div> <div class="chapter" title="Chapter 4. Variables"> <div class="titlepage"><div><div><h2 class="title"> <a name="language.variables"></a>Chapter 4. Variables</h2></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <dl> <dt><span class="sect1"><a href="language.variables.html#language.assigned.variables">Variables assigned from PHP</a></span></dt> <dd><dl> <dt><span class="sect2"><a href="language.variables.html#language.variables.assoc.arrays">Associative arrays</a></span></dt> <dt><span class="sect2"><a href="language.variables.html#language.variables.array.indexes">Array indexes</a></span></dt> <dt><span class="sect2"><a href="language.variables.html#language.variables.objects">Objects</a></span></dt> </dl></dd> <dt><span class="sect1"><a href="language.config.variables.html">Variables loaded from config files</a></span></dt> <dt><span class="sect1"><a href="language.variables.smarty.html">{$smarty} reserved variable</a></span></dt> <dd><dl> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.request">Request variables</a></span></dt> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.now">{$smarty.now}</a></span></dt> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.const">{$smarty.const}</a></span></dt> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.capture">{$smarty.capture}</a></span></dt> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.config">{$smarty.config}</a></span></dt> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.loops">{$smarty.section}, {$smarty.foreach}</a></span></dt> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.template">{$smarty.template}</a></span></dt> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.version">{$smarty.version}</a></span></dt> <dt><span class="sect2"><a href="language.variables.smarty.html#language.variables.smarty.ldelim">{$smarty.ldelim}, {$smarty.rdelim}</a></span></dt> </dl></dd> </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 class="link" href="language.syntax.functions.html" title="Functions">functions</a>, <a class="link" href="language.syntax.attributes.html" title="Attributes">attributes</a> and <a class="link" href="language.modifiers.html" title="Chapter 5. Variable Modifiers">modifiers</a>, inside conditional expressions, etc. To print a variable, simply enclose it in the <a class="link" href="variable.left.delimiter.html" title="$left_delimiter">delimiters</a> so that it is the only thing contained between them. </p> <div class="example"> <a name="id3026588"></a><p class="title"><b>Example 4.1. Example variables</b></p> <div class="example-contents"><pre class="programlisting"> {$Name} {$product.part_no} <b>{$product.description}</b> {$Contacts[row].Phone} <body bgcolor="{#bgcolor#}"> </pre></div> </div> <p><br class="example-break"> </p> <div class="note" title="Top Tip" style="margin-left: 0.5in; margin-right: 0.5in;"> <h3 class="title">Top Tip</h3> <p>An easy way to examine Smarty variables is with the <a class="link" href="chapter.debugging.console.html" title="Chapter 10. Debugging Console">debugging console</a>. </p> </div> <p> </p> <div class="sect1" title="Variables assigned from PHP"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="language.assigned.variables"></a>Variables assigned from PHP</h2></div></div></div> <p> Variables that are <a class="link" href="api.assign.html" title="assign()">assigned</a> from PHP are referenced by preceding them with a dollar sign <code class="literal">$</code> (like php). Variables assigned from within a template with the <a class="link" href="language.custom.functions.html#language.function.assign" title="{assign}"><code class="varname">{assign}</code></a> function are also displayed this way. </p> <div class="example"> <a name="id3026701"></a><p class="title"><b>Example 4.2. Assigned variables</b></p> <div class="example-contents"> <p> php script</p> <pre class="programlisting"> <?php $smarty = new Smarty(); $smarty->assign('firstname', 'Doug'); $smarty->assign('lastname', 'Evans'); $smarty->assign('meetingPlace', 'New York'); $smarty->display('index.tpl'); ?> </pre> <p> where the content of <code class="filename">index.tpl</code> is: </p> <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> <p> This above would output: </p> <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> </div> </div> <br class="example-break"><div class="sect2" title="Associative arrays"> <div class="titlepage"><div><div><h3 class="title"> <a name="language.variables.assoc.arrays"></a>Associative arrays</h3></div></div></div> <p> You can also reference associative array variables that are assigned from PHP by specifying the key after the '.' (period) symbol. </p> <div class="example"> <a name="id3030090"></a><p class="title"><b>Example 4.3. Accessing associative array variables</b></p> <div class="example-contents"> <pre class="programlisting"> <?php $smarty->assign('Contacts', array('fax' => '555-222-9876', 'email' => 'zaphod@slartibartfast.example.com', 'phone' => array('home' => '555-444-3333', 'cell' => '555-111-1234') ) ); $smarty->display('index.tpl'); ?> </pre> <p> where the content of <code class="filename">index.tpl</code> is: </p> <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> <p> this will output: </p> <pre class="screen"> 555-222-9876<br /> zaphod@slartibartfast.example.com<br /> 555-444-3333<br /> 555-111-1234<br /> </pre> </div> </div> <br class="example-break"> </div> <div class="sect2" title="Array indexes"> <div class="titlepage"><div><div><h3 class="title"> <a name="language.variables.array.indexes"></a>Array indexes</h3></div></div></div> <p> You can reference arrays by their index, much like native PHP syntax. </p> <div class="example"> <a name="id3030161"></a><p class="title"><b>Example 4.4. Accessing arrays by index</b></p> <div class="example-contents"> <pre class="programlisting"> <?php $smarty->assign('Contacts', array( '555-222-9876', 'zaphod@slartibartfast.example.com', array('555-444-3333', '555-111-1234') )); $smarty->display('index.tpl'); ?> </pre> <p> where <code class="filename">index.tpl</code> is: </p> <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> <p> This will output: </p> <pre class="screen"> 555-222-9876<br /> zaphod@slartibartfast.example.com<br /> 555-444-3333<br /> 555-111-1234<br /> </pre> </div> </div> <br class="example-break"> </div> <div class="sect2" title="Objects"> <div class="titlepage"><div><div><h3 class="title"> <a name="language.variables.objects"></a>Objects</h3></div></div></div> <p> Properties of <a class="link" href="advanced.features.html#advanced.features.objects" title="Objects">objects</a> assigned from PHP can be referenced by specifying the property name after the <code class="literal">-></code> symbol. </p> <div class="example"> <a name="id3030240"></a><p class="title"><b>Example 4.5. Accessing object properties</b></p> <div class="example-contents"> <pre class="programlisting"> name: {$person->name}<br /> email: {$person->email}<br /> </pre> <p> this will output: </p> <pre class="screen"> name: Zaphod Beeblebrox<br /> email: zaphod@slartibartfast.example.com<br /> </pre> </div> </div> <br class="example-break"> </div> </div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="language.escaping.html">Prev</a> </td> <td width="20%" align="center"><a accesskey="u" href="smarty.for.designers.html">Up</a></td> <td width="40%" align="right"> <a accesskey="n" href="language.config.variables.html">Next</a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Escaping Smarty Parsing </td> <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td> <td width="40%" align="right" valign="top"> Variables loaded from config files</td> </tr> </table> </div> </body> </html>