<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="Basic Syntax" HREF="language.basic.syntax.html"><LINK REL="PREVIOUS" TITLE="Basic Syntax" HREF="language.basic.syntax.html"><LINK REL="NEXT" TITLE="Functions" HREF="language.syntax.functions.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" 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.basic.syntax.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 3. Basic Syntax</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.syntax.functions.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.syntax.variables" ></A >Variables</H1 ><P > Template variables start with the $dollar sign. They can contain numbers, letters and underscores, much like a <A 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 HREF="language.config.variables.html" >Config file variables</A > are an exception to the $dollar syntax and are instead referenced with surrounding #hashmarks#, or via the <A HREF="language.variables.smarty.html#language.variables.smarty.config" > <TT CLASS="parameter" ><I >$smarty.config</I ></TT ></A > variable. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN240" ></A ><P ><B >Example 3-2. Variables</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><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 ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >Request variables such as <TT CLASS="literal" >$_GET</TT >, <TT CLASS="literal" >$_SESSION</TT >, etc are available via the reserved <A HREF="language.variables.smarty.html" > <TT CLASS="parameter" ><I >$smarty</I ></TT ></A > variable. </P ><P > See also <A HREF="language.variables.smarty.html" > <TT CLASS="parameter" ><I >$smarty</I ></TT ></A >, <A HREF="language.config.variables.html" >config variables</A > <A HREF="language.custom.functions.html#language.function.assign" ><TT CLASS="varname" >{assign}</TT ></A > and <A HREF="api.assign.html" ><TT CLASS="varname" >assign()</TT ></A >. </P ></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.basic.syntax.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.syntax.functions.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Basic Syntax</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.basic.syntax.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Functions</TD ></TR ></TABLE ></DIV ></BODY ></HTML >