Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > by-pkgid > 8377d4bb25a3992aad680df2952f4b71 > files > 120

php-smarty-doc-2.6.26-1mdv2010.0.noarch.rpm

<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
>&#13;  Smarty has several different types of variables. The type of the variable
  depends on what symbol it is prefixed or enclosed within.
 </P
><P
>&#13;  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} &#60;b&#62;{$product.description}&#60;/b&#62;

{$Contacts[row].Phone}

&#60;body bgcolor="{#bgcolor#}"&#62;</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
>&#13;  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">&lt;?php<br /><br />$smarty&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Smarty</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'firstname'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Doug'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'lastname'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Evans'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'meetingPlace'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'New&nbsp;York'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-&gt;</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">?&gt;</span>
</span>
</code></TD
></TR
></TABLE
><P
>&#13;      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.
&#60;br /&#62;
{* 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
>&#13;   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.
&#60;br /&#62;
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
>&#13;   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">&lt;?php<br />$smarty</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contacts'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'fax'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'555-222-9876'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'email'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'zaphod@slartibartfast.example.com'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'phone'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'home'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'555-444-3333'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'cell'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'555-111-1234'</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-&gt;</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">?&gt;</span>
</span>
</code></TD
></TR
></TABLE
><P
>&#13;    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}&#60;br /&#62;
{$Contacts.email}&#60;br /&#62;
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}&#60;br /&#62;
{$Contacts.phone.cell}&#60;br /&#62;</PRE
></TD
></TR
></TABLE
><P
>&#13;    this will output:
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>555-222-9876&#60;br /&#62;
zaphod@slartibartfast.example.com&#60;br /&#62;
555-444-3333&#60;br /&#62;
555-111-1234&#60;br /&#62;</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
>&#13;   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">&lt;?php<br />$smarty</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contacts'</span><span style="color: #007700">,&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'555-222-9876'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'zaphod@slartibartfast.example.com'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'555-444-3333'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'555-111-1234'</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;));<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-&gt;</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">?&gt;</span>
</span>
</code></TD
></TR
></TABLE
><P
>&#13;    where <TT
CLASS="filename"
>index.tpl</TT
> is:
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>{$Contacts[0]}&#60;br /&#62;
{$Contacts[1]}&#60;br /&#62;
{* you can print arrays of arrays as well *}
{$Contacts[2][0]}&#60;br /&#62;
{$Contacts[2][1]}&#60;br /&#62;</PRE
></TD
></TR
></TABLE
><P
>&#13;    This will output:
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>555-222-9876&#60;br /&#62;
zaphod@slartibartfast.example.com&#60;br /&#62;
555-444-3333&#60;br /&#62;
555-111-1234&#60;br /&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.variables.objects"
></A
>Objects</H2
><P
>&#13;   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"
>-&#62;</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-&#62;name}&#60;br /&#62;
email: {$person-&#62;email}&#60;br /&#62;</PRE
></TD
></TR
></TABLE
><P
>&#13;    this will output:
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>name:  Zaphod Beeblebrox&#60;br /&#62;
email: zaphod@slartibartfast.example.com&#60;br /&#62;</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
>