Files
smarty/docs/en/designers/language-variables.xml
2004-03-28 16:48:54 +00:00

417 lines
10 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<chapter id="language.variables">
<title>Variables</title>
<para>
Smarty has several different types of variables. The type of the variable
depends on what symbol it is prefixed with (or enclosed within).
</para>
<para>
Variables in Smarty can be either displayed directly or used as arguments
for function attributes and modifiers, inside conditional expressions,
etc. To print a variable, simply enclose it in the delimiters so that it
is the only thing contained between them. Examples:
<programlisting>
<![CDATA[
{$Name}
{$Contacts[row].Phone}
<body bgcolor="{#bgcolor#}">
]]>
</programlisting>
</para>
<sect1 id="language.assigned.variables">
<title>Variables assigned from PHP</title>
<para>
Variables that are assigned from PHP are referenced by preceding them with
a dollar sign <literal>$</literal>. Variables assigned from within the
template with the <link linkend="language.function.assign">assign</link>
function are also displayed this way.
</para>
<example>
<title>assigned variables</title>
<programlisting>
<![CDATA[
Hello {$firstname}, glad to see you could make it.
<br />
Your last login was on {$lastLoginDate}.
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Hello Doug, glad to see you could make it.
<br />
Your last login was on January 11th, 2001.
]]>
</screen>
</example>
<sect2 id="language.variables.assoc.arrays">
<title>Associative arrays</title>
<para>
You can also reference associative array variables that are
assigned from PHP by specifying the key after the '.' (period)
symbol.
</para>
<example>
<title>accessing associative array variables</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('Contacts',
array('fax' => '555-222-9876',
'email' => 'zaphod@slartibartfast.com',
'phone' => array('home' => '555-444-3333',
'cell' => '555-111-1234')));
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
where the content of index.tpl is:
</para>
<programlisting>
<![CDATA[
{$Contacts.fax}<br />
{$Contacts.email}<br />
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}<br />
{$Contacts.phone.cell}<br />
]]>
</programlisting>
<para>
this will output:
</para>
<screen>
<![CDATA[
555-222-9876<br />
zaphod@slartibartfast.com<br />
555-444-3333<br />
555-111-1234<br />
]]>
</screen>
</example>
</sect2>
<sect2 id="language.variables.array.indexes">
<title>Array indexes</title>
<para>
You can reference arrays by their index, much like native PHP
syntax.
</para>
<example>
<title>accessing arrays by index</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('Contacts',
array('555-222-9876',
'zaphod@slartibartfast.com',
array('555-444-3333',
'555-111-1234')));
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$Contacts[0]}<br />
{$Contacts[1]}<br />
{* you can print arrays of arrays as well *}
{$Contacts[2][0]}<br />
{$Contacts[2][1]}<br />
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
555-222-9876<br />
zaphod@slartibartfast.com<br />
555-444-3333<br />
555-111-1234<br />
]]>
</screen>
</example>
</sect2>
<sect2 id="language.variables.objects">
<title>Objects</title>
<para>
Properties of objects assigned from PHP can be referenced
by specifying the property name after the '-&gt;' symbol.
</para>
<example>
<title>accessing object properties</title>
<programlisting>
<![CDATA[
name: {$person-&gt;name}<br />
email: {$person-&gt;email}<br />
]]>
</programlisting>
<para>
this will output:
</para>
<screen>
<![CDATA[
name: Zaphod Beeblebrox<br />
email: zaphod@slartibartfast.com<br />
]]>
</screen>
</example>
</sect2>
</sect1>
<sect1 id="language.config.variables">
<title>Variables loaded from config files</title>
<para>
Variables that are loaded from the config files are referenced by
enclosing them within hash marks (#), or with the smarty variable
<link
linkend="language.variables.smarty.config">$smarty.config</link>.
The second syntax is useful for embedding into quoted attribute
values.
</para>
<example>
<title>config variables</title>
<para>
foo.conf:
</para>
<programlisting>
<![CDATA[
pageTitle = "This is mine"
bodyBgColor = "#eeeeee"
tableBorderSize = "3"
tableBgColor = "#bbbbbb"
rowBgColor = "#cccccc"
]]>
</programlisting>
<para>
index.tpl:
</para>
<programlisting>
<![CDATA[
{config_load file="foo.conf"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
]]>
</programlisting>
<para>
index.tpl: (alternate syntax)
</para>
<programlisting>
<![CDATA[
{config_load file="foo.conf"}
<html>
<title>{$smarty.config.pageTitle}</title>
<body bgcolor="{$smarty.config.bodyBgColor}">
<table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor}">
<tr bgcolor="{$smarty.config.rowBgColor}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
]]>
</programlisting>
<para>
this will output for both examples:
</para>
<screen>
<![CDATA[
<html>
<title>This is mine</title>
<body bgcolor="#eeeeee">
<table border="3" bgcolor="#bbbbbb">
<tr bgcolor="#cccccc">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
]]>
</screen>
</example>
<para>
Config file variables cannot be used until
after they are loaded in from a config file. This procedure is
explained later in this document under <command>config_load</command>.
</para>
</sect1>
<sect1 id="language.variables.smarty">
<title>{$smarty} reserved variable</title>
<para>
The reserved {$smarty} variable can be used to access several
special template variables. The full list of them follows.
</para>
<sect2 id="language.variables.smarty.request">
<title>Request variables</title>
<para>
The request variables such as get, post, cookies, server,
environment, and session variables can be accessed as demonstrated
in the examples below:
</para>
<example>
<title>displaying request variables</title>
<programlisting>
<![CDATA[
{* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *}
{$smarty.get.page}
{* display the variable "page" from a form (POST) *}
{$smarty.post.page}
{* display the value of the cookie "username" *}
{$smarty.cookies.username}
{* display the server variable "SERVER_NAME" *}
{$smarty.server.SERVER_NAME}
{* display the system environment variable "PATH" *}
{$smarty.env.PATH}
{* display the php session variable "id" *}
{$smarty.session.id}
{* display the variable "username" from merged get/post/cookies/server/env *}
{$smarty.request.username}
]]>
</programlisting>
</example>
<note>
<para>
For historical reasons {$SCRIPT_NAME} can be accessed
directly though {$smarty.server.SCRIPT_NAME} is the
proposed way to access this value.
</para>
</note>
</sect2>
<sect2 id="language.variables.smarty.now">
<title>{$smarty.now}</title>
<para>
The current timestamp can be accessed with {$smarty.now}. The
number reflects the number of seconds passed since the so-called
Epoch (January 1, 1970) and can be passed directly to
date_format modifier for display purposes.
</para>
<example>
<title>using {$smarty.now}</title>
<programlisting>
<![CDATA[
{* use the date_format modifier to show current date and time *}
{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
]]>
</programlisting>
</example>
</sect2>
<sect2 id="language.variables.smarty.const">
<title>{$smarty.const}</title>
<para>
You can access PHP constant values directly.
</para>
<example>
<title>using {$smarty.const}</title>
<programlisting>
<![CDATA[
{$smarty.const._MY_CONST_VAL}
]]>
</programlisting>
</example>
</sect2>
<sect2 id="language.variables.smarty.capture">
<title>{$smarty.capture}</title>
<para>
The output captured via {capture}..{/capture} construct can be
accessed using {$smarty} variable. See section on
<link linkend="language.function.capture">capture</link> for an example.
</para>
</sect2>
<sect2 id="language.variables.smarty.config">
<title>{$smarty.config}</title>
<para>
{$smarty} variable can be used to refer to loaded config variables.
{$smarty.config.foo} is a synonym for {#foo#}. See the section on
<link linkend="language.function.config.load">config_load</link> for an example.
</para>
</sect2>
<sect2 id="language.variables.smarty.loops">
<title>{$smarty.section}, {$smarty.foreach}</title>
<para>
{$smarty} variable can be used to refer to 'section' and
'foreach' loop properties. See docs for
<link linkend="language.function.section">section</link> and
<link linkend="language.function.foreach">foreach</link>.
</para>
</sect2>
<sect2 id="language.variables.smarty.template">
<title>{$smarty.template}</title>
<para>
This variable contains the name of the current template being
processed.
</para>
</sect2>
<sect2 id="language.variables.smarty.version">
<title>{$smarty.version}</title>
<para>
This variable contains the version of Smarty the template was compiled with.
</para>
</sect2>
</sect1>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->