Tidy up formatting on variables

This commit is contained in:
pete_morgan
2006-09-26 03:44:02 +00:00
parent 253fd367bb
commit b0507f2ab3
4 changed files with 130 additions and 91 deletions

View File

@@ -8,23 +8,35 @@
</para>
<para>
Variables in Smarty can be either displayed directly or used as arguments
for <link linkend="language.syntax.functions">function</link>
for <link linkend="language.syntax.functions">function</link>,
<link linkend="language.syntax.attributes">attributes</link> and
<link linkend="language.modifiers">modifiers</link>, inside conditional expressions,
etc. To print a variable, simply enclose it in the
<link linkend="variable.left.delimiter">delimiters</link> so that it
is the only thing contained between them. Examples:
is the only thing contained between them.
<example>
<title>Example variables</title>
<programlisting>
<![CDATA[
{$Name}
{$product.part_no} <b>{$product.description}</b>
{$Contacts[row].Phone}
<body bgcolor="{#bgcolor#}">
]]>
</programlisting>
</example>
<note>
<title>Top Tip</title>
<para>As easy way to examine Smarty variables is with the
<link linkend="chapter.debugging.console">debugging console</link>.
</para>
</note>
</para>
&designers.language-variables.language-assigned-variables;
&designers.language-variables.language-config-variables;
&designers.language-variables.language-variables-smarty;

View File

@@ -4,19 +4,21 @@
<title>Variables assigned from PHP</title>
<para>
Variables that are
<link linkend="api.assign">assigned</link> from PHP are referenced by preceding them with
a dollar sign <literal>$</literal>. Variables assigned from within a
template with the <link linkend="language.function.assign">{assign}</link>
<link linkend="api.assign">assigned</link> from PHP are referenced by
preceding them with a dollar sign <literal>$</literal> (like php).
Variables assigned from within a template with the
<link linkend="language.function.assign"><varname>{assign}</varname></link>
function are also displayed this way.
</para>
<example>
<title>assigned variables</title>
<title>Assigned variables</title>
<para> php script</para>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty = new Smarty();
$smarty->assign('firstname', 'Doug');
$smarty->assign('lastname', 'Evans');
@@ -28,13 +30,13 @@ $smarty->display('index.tpl');
]]>
</programlisting>
<para>
where the content of index.tpl is:
where the content of <filename>index.tpl</filename> is:
</para>
<programlisting>
<![CDATA[
Hello {$firstname} {$lastname}, glad to see you can make it.
<br />
{* this will not work as $vars are case sensitive *}
{* this will not work as $variables are case sensitive *}
This weeks meeting is in {$meetingplace}.
{* this will work *}
This weeks meeting is in {$meetingPlace}.
@@ -42,7 +44,7 @@ This weeks meeting is in {$meetingPlace}.
</programlisting>
<para>
This will output:
This above would output:
</para>
<screen>
<![CDATA[
@@ -53,6 +55,8 @@ This weeks meeting is in New York.
]]>
</screen>
</example>
<sect2 id="language.variables.assoc.arrays">
<title>Associative arrays</title>
<para>
@@ -61,7 +65,7 @@ This weeks meeting is in New York.
symbol.
</para>
<example>
<title>accessing associative array variables</title>
<title>Accessing associative array variables</title>
<programlisting role="php">
<![CDATA[
<?php
@@ -77,7 +81,7 @@ $smarty->display('index.tpl');
]]>
</programlisting>
<para>
where the content of index.tpl is:
where the content of <filename>index.tpl</filename> is:
</para>
<programlisting>
<![CDATA[
@@ -101,14 +105,15 @@ zaphod@slartibartfast.example.com<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.
You can reference arrays by their index, much like native PHP syntax.
</para>
<example>
<title>accessing arrays by index</title>
<title>Accessing arrays by index</title>
<programlisting role="php">
<![CDATA[
<?php
@@ -123,7 +128,7 @@ $smarty->display('index.tpl');
]]>
</programlisting>
<para>
where index.tpl is:
where <filename>index.tpl</filename> is:
</para>
<programlisting>
<![CDATA[
@@ -147,18 +152,20 @@ zaphod@slartibartfast.example.com<br />
</screen>
</example>
</sect2>
<sect2 id="language.variables.objects">
<title>Objects</title>
<para>
Properties of <link linkend="advanced.features.objects">objects</link>
assigned from PHP can be referenced by specifying the property
name after the '-&gt;' symbol.
name after the <literal>-&gt;</literal> symbol.
</para>
<example>
<title>accessing object properties</title>
<title>Accessing object properties</title>
<programlisting>
<![CDATA[
name: {$person->name}<br />
name: {$person->name}<br />
email: {$person->email}<br />
]]>
</programlisting>
@@ -167,7 +174,7 @@ email: {$person->email}<br />
</para>
<screen>
<![CDATA[
name: Zaphod Beeblebrox<br />
name: Zaphod Beeblebrox<br />
email: zaphod@slartibartfast.example.com<br />
]]>
</screen>

View File

@@ -1,45 +1,44 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="language.config.variables">
<title>Variables loaded from config files</title>
<para>
<title>Variables loaded from config files</title>
<para>
Variables that are loaded from the
<link linkend="config.files">config files</link>
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>
are referenced by enclosing them within <literal>#hash marks#</literal>,
or with the smarty variable
<link linkend="language.variables.smarty.config">
<parameter>$smarty.config</parameter></link>.
The later syntax is useful for embedding into quoted attribute values.
</para>
<example>
<title>config variables</title>
<para>
foo.conf:
Example config file - <filename>foo.conf</filename>:
</para>
<programlisting>
<![CDATA[
pageTitle = "This is mine"
bodyBgColor = "#eeeeee"
tableBorderSize = "3"
bodyBgColor = '#eeeeee'
tableBorderSize = 3
tableBgColor = "#bbbbbb"
rowBgColor = "#cccccc"
]]>
</programlisting>
<para>
index.tpl:
A template demonstrating the <parameter>#hash#</parameter> method:
</para>
<programlisting>
<![CDATA[
{config_load file="foo.conf"}
{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>
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
@@ -47,19 +46,21 @@ rowBgColor = "#cccccc"
]]>
</programlisting>
<para>
index.tpl: (alternate syntax)
A template demonstrating the
<link linkend="language.variables.smarty.config">
<parameter>$smarty.config</parameter></link> method:
</para>
<programlisting>
<![CDATA[
{config_load file="foo.conf"}
{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>
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
@@ -67,7 +68,7 @@ rowBgColor = "#cccccc"
]]>
</programlisting>
<para>
this will output for both examples:
Both examples would output:
</para>
<screen>
<![CDATA[
@@ -90,7 +91,8 @@ rowBgColor = "#cccccc"
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
<link linkend="language.function.config.load"><command>{config_load}</command></link>.
<link linkend="language.function.config.load">
<varname>{config_load}</varname></link>.
</para>
<para>
See also <link linkend="language.syntax.variables">Variables</link> and

View File

@@ -3,21 +3,26 @@
<sect1 id="language.variables.smarty">
<title>{$smarty} reserved variable</title>
<para>
The PHP reserved {$smarty} variable can be used to access several
enviroment and request variables. The full list of them follows.
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 $_GET, $_POST, $_COOKIE, $_SERVER, $_ENV and $_SESSION
(see <link linkend="variable.request.vars.order">$request_vars_order</link>
and <link
linkend="variable.request.use.auto.globals">$request_use_auto_globals</link>
) can be accessed as demonstrated in the examples below:
</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>
<title>Displaying request variables</title>
<programlisting>
<![CDATA[
{* display value of page from URL ($_GET) http://www.example.com/index.php?page=foo *}
@@ -45,8 +50,8 @@
</example>
<note>
<para>
For historical reasons {$SCRIPT_NAME} can be accessed
directly, although {$smarty.server.SCRIPT_NAME} is the
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>
@@ -57,45 +62,53 @@
</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 {$smarty.now}. The value reflects the number of
seconds passed since the so-called Epoch (January 1, 1970)
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">date_format</link>
modifier for display purposes. Note that time() is called each invocation.
ie a script that takes 3 seconds to execute with $smarty.now at start and end will show the 3 seconds difference.
<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 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.
</para>
<example>
<title>using {$smarty.now}</title>
<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"}
{$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. See also <link
linkend="smarty.constants">smarty constants</link>
linkend="smarty.constants">smarty constants</link>.
</para>
<example>
<title>using {$smarty.const} to access constants</title>
<title>Using {$smarty.const} to access a constant</title>
<programlisting role="php">
<![CDATA[
<?php
// the constant defined in php
define('_MY_CONST_VAL','CHERRIES');
define('MY_CONST_VAL','CHERRIES');
?>
]]>
</programlisting>
<para>Output the constant in a template with</para>
<programlisting>
<![CDATA[
{* the output of php constant in the template *}
{$smarty.const._MY_CONST_VAL}
{$smarty.const.MY_CONST_VAL}
]]>
</programlisting>
</example>
@@ -104,41 +117,47 @@ define('_MY_CONST_VAL','CHERRIES');
<sect2 id="language.variables.smarty.capture">
<title>{$smarty.capture}</title>
<para>
Template output captured via the <link
linkend="language.function.capture">{capture}..{/capture}</link>
construct can be accessed using the {$smarty.capture} variable. See the section on
<link linkend="language.function.capture">{capture}</link> for an
example.
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>
{$smarty} variable can be used to refer to loaded <link
linkend="language.config.variables">config variables</link>.
{$smarty.config.foo} is a synonym for {#foo#}. See the section on
<link linkend="language.function.config.load">{config_load}</link>
for an example.
<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>
{$smarty} variable can be used to refer to
<link linkend="language.function.section">{section}</link> and
<link linkend="language.function.foreach">{foreach}</link>
loop properties. These has very useful values such as .first .index etc
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. This example shows the container.tpl and the included banner.tpl
with {$smarty.template} within both
Returns the name of the current template being processed.
The following example shows the <filename>container.tpl</filename> and the
<filename>included banner.tpl</filename> with
<parameter>{$smarty.template}</parameter> within both.
</para>
<programlisting>
<![CDATA[
@@ -156,8 +175,7 @@ banner.tpl
]]>
</programlisting>
</sect2>
<sect2 id="language.variables.smarty.version">
<title>{$smarty.version}</title>
<para>
@@ -168,19 +186,19 @@ banner.tpl
<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">{ldelim},{rdelim}</link>.
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.syntax.variables">Variables</link> and
<link linkend="language.config.variables">Config Variables</link>
<link linkend="language.assigned.variables">assigned variables</link> and
<link linkend="language.config.variables">config variables</link>
</para>
</sect2>
</sect1>