Files
smarty/docs/en/designers/language-variables/language-variables-smarty.xml
2008-12-23 20:32:55 +00:00

225 lines
6.9 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- $Revision$ -->
<sect1 id="language.variables.smarty">
<title>{$smarty} reserved variable</title>
<para>
The PHP reserved <parameter>{$smarty}</parameter> variable can be used to
access several enviroment and request variables.
The full list of them follows.
</para>
<sect2 id="language.variables.smarty.request">
<title>Request variables</title>
<para>
The <ulink url="&url.php-manual;reserved.variables">request variables
</ulink> such as <literal>$_GET</literal>, <literal>$_POST</literal>,
<literal>$_COOKIE</literal>, <literal>$_SERVER</literal>,
<literal>$_ENV</literal> and <literal>$_SESSION</literal>
(see <link linkend="variable.request.vars.order">
<varname>$request_vars_order</varname></link>
and <link linkend="variable.request.use.auto.globals">
<varname>$request_use_auto_globals</varname></link>)
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.example.com/index.php?page=foo *}
{$smarty.get.page}
{* display the variable "page" from a form ($_POST['page']) *}
{$smarty.post.page}
{* display the value of the cookie "username" ($_COOKIE['username']) *}
{$smarty.cookies.username}
{* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*}
{$smarty.server.SERVER_NAME}
{* display the system environment variable "PATH" *}
{$smarty.env.PATH}
{* display the php session variable "id" ($_SESSION['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 <parameter>{$SCRIPT_NAME}</parameter> can be accessed
directly, although <parameter>{$smarty.server.SCRIPT_NAME}</parameter> is the
proposed way to access this value.
</para>
<programlisting>
<![CDATA[
<a href="{$SCRIPT_NAME}?page=smarty">click me</a>
<a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>
]]>
</programlisting>
</note>
</sect2>
<sect2 id="language.variables.smarty.now">
<title>{$smarty.now}</title>
<para>
The current <ulink url="&url.php-manual;function.time">timestamp</ulink>
can be accessed with <parameter>{$smarty.now}</parameter>.
The value reflects the number of
seconds passed since the so-called Epoch on January 1, 1970,
and can be passed directly to the
<link linkend="language.modifier.date.format"><varname>date_format</varname>
</link> modifier for display. Note that
<ulink url="&url.php-manual;function.time"><varname>time()</varname></ulink>
is called on each invocation; eg a script that takes three seconds to execute
with a call to <parameter>$smarty.now</parameter> at start and end
will show the three second difference.
<informalexample>
<programlisting>
<![CDATA[
{* use the date_format modifier to show current date and time *}
{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}
]]>
</programlisting>
</informalexample>
</para>
</sect2>
<sect2 id="language.variables.smarty.const">
<title>{$smarty.const}</title>
<para>
You can access PHP constant values directly. See also <link
linkend="smarty.constants">smarty constants</link>.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// the constant defined in php
define('MY_CONST_VAL','CHERRIES');
?>
]]>
</programlisting>
</informalexample>
<para>Output the constant in a template with</para>
<informalexample>
<programlisting>
<![CDATA[
{$smarty.const.MY_CONST_VAL}
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 id="language.variables.smarty.capture">
<title>{$smarty.capture}</title>
<para>
Template output captured via the built-in
<link linkend="language.function.capture">
<varname>{capture}..{/capture}</varname></link> function can be accessed
using the <parameter>{$smarty.capture}</parameter> variable.
See the <link linkend="language.function.capture">
<varname>{capture}</varname></link> page for more information.
</para>
</sect2>
<sect2 id="language.variables.smarty.config">
<title>{$smarty.config}</title>
<para>
<parameter>{$smarty.config}</parameter> variable can be used to refer
to loaded <link linkend="language.config.variables">config variables</link>.
<parameter>{$smarty.config.foo}</parameter> is a synonym for
<parameter>{#foo#}</parameter>. See the
<link linkend="language.function.config.load">{config_load}</link> page
for more info.
</para>
</sect2>
<sect2 id="language.variables.smarty.loops">
<title>{$smarty.section}, {$smarty.foreach}</title>
<para>
The <parameter>{$smarty.section}</parameter> and
<parameter>{$smarty.foreach}</parameter> variables can be used to refer to
<link linkend="language.function.section"><varname>{section}</varname></link>
and
<link linkend="language.function.foreach"><varname>{foreach}</varname></link>
loop properties respectfully. These have some very useful values such as
<varname>.first</varname>, <varname>.index</varname>, etc.
</para>
</sect2>
<sect2 id="language.variables.smarty.template">
<title>{$smarty.template}</title>
<para>
Returns the name of the current template being processed.
The following example shows the <filename>container.tpl</filename> and the
included <filename>banner.tpl</filename> with
<parameter>{$smarty.template}</parameter> within both.
</para>
<programlisting>
<![CDATA[
<b>Main container is {$smarty.template}</b>
{include file='banner.tpl'}
]]>
</programlisting>
<para>
will output
</para>
<programlisting>
<![CDATA[
<b>Main page is container.tpl</b>
banner.tpl
]]>
</programlisting>
</sect2>
<sect2 id="language.variables.smarty.version">
<title>{$smarty.version}</title>
<para>
Returns the version of Smarty the template was compiled with.
</para>
<programlisting>
<![CDATA[
<div id="footer">Powered by Smarty {$smarty.version}</div>
]]>
</programlisting>
</sect2>
<sect2 id="language.variables.smarty.ldelim">
<title>{$smarty.ldelim}, {$smarty.rdelim}</title>
<para>
These variables are used for printing the left-delimiter and right-delimiter
value literally, the same as <link linkend="language.function.ldelim">
<varname>{ldelim},{rdelim}</varname></link>.
</para>
<para>
See also
<link linkend="language.assigned.variables">assigned variables</link> and
<link linkend="language.config.variables">config variables</link>
</para>
</sect2>
</sect1>
<!-- 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
-->