<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>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="language.basic.syntax.html" title="Chapter 3. Basic Syntax"> <link rel="prev" href="language.basic.syntax.html" title="Chapter 3. Basic Syntax"> <link rel="next" href="language.syntax.functions.html" title="Functions"> </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">Variables</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="language.basic.syntax.html">Prev</a> </td> <th width="60%" align="center">Chapter 3. Basic Syntax</th> <td width="20%" align="right"> <a accesskey="n" href="language.syntax.functions.html">Next</a> </td> </tr> </table> <hr> </div> <div class="sect1" title="Variables"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="language.syntax.variables"></a>Variables</h2></div></div></div> <p> Template variables start with the $dollar sign. They can contain numbers, letters and underscores, much like a <a class="ulink" href="http://php.net/language.variables" target="_top">PHP variable</a>. You can reference arrays by index numerically or non-numerically. Also reference object properties and methods.</p> <p> <a class="link" href="language.config.variables.html" title="Variables loaded from config files">Config file variables</a> are an exception to the $dollar syntax and are instead referenced with surrounding #hashmarks#, or via the <a class="link" href="language.variables.smarty.html#language.variables.smarty.config" title="{$smarty.config}"> <em class="parameter"><code>$smarty.config</code></em></a> variable. </p> <div class="example"> <a name="id3026799"></a><p class="title"><b>Example 3.2. Variables</b></p> <div class="example-contents"><pre class="programlisting"> {$foo} <-- displaying a simple variable (non array/object) {$foo[4]} <-- display the 5th element of a zero-indexed array {$foo.bar} <-- display the "bar" key value of an array, similar to PHP $foo['bar'] {$foo.$bar} <-- display variable key value of an array, similar to PHP $foo[$bar] {$foo->bar} <-- display the object property "bar" {$foo->bar()} <-- display the return value of object method "bar" {#foo#} <-- display the config file variable "foo" {$smarty.config.foo} <-- synonym for {#foo#} {$foo[bar]} <-- syntax only valid in a section loop, see {section} {assign var=foo value='baa'}{$foo} <-- displays "baa", see {assign} Many other combinations are allowed {$foo.bar.baz} {$foo.$bar.$baz} {$foo[4].baz} {$foo[4].$baz} {$foo.bar.baz[4]} {$foo->bar($baz,2,$bar)} <-- passing parameters {"foo"} <-- static values are allowed {* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*} {$smarty.server.SERVER_NAME} </pre></div> </div> <br class="example-break"><p>Request variables such as <code class="literal">$_GET</code>, <code class="literal">$_SESSION</code>, etc are available via the reserved <a class="link" href="language.variables.smarty.html" title="{$smarty} reserved variable"> <em class="parameter"><code>$smarty</code></em></a> variable. </p> <p> See also <a class="link" href="language.variables.smarty.html" title="{$smarty} reserved variable"> <em class="parameter"><code>$smarty</code></em></a>, <a class="link" href="language.config.variables.html" title="Variables loaded from config files">config variables</a> <a class="link" href="language.custom.functions.html#language.function.assign" title="{assign}"><code class="varname">{assign}</code></a> and <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a>. </p> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="language.basic.syntax.html">Prev</a> </td> <td width="20%" align="center"><a accesskey="u" href="language.basic.syntax.html">Up</a></td> <td width="40%" align="right"> <a accesskey="n" href="language.syntax.functions.html">Next</a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Chapter 3. Basic Syntax </td> <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td> <td width="40%" align="right" valign="top"> Functions</td> </tr> </table> </div> </body> </html>