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>
<para> <para>
Variables in Smarty can be either displayed directly or used as arguments 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.syntax.attributes">attributes</link> and
<link linkend="language.modifiers">modifiers</link>, inside conditional expressions, <link linkend="language.modifiers">modifiers</link>, inside conditional expressions,
etc. To print a variable, simply enclose it in the etc. To print a variable, simply enclose it in the
<link linkend="variable.left.delimiter">delimiters</link> so that it <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> <programlisting>
<![CDATA[ <![CDATA[
{$Name} {$Name}
{$product.part_no} <b>{$product.description}</b>
{$Contacts[row].Phone} {$Contacts[row].Phone}
<body bgcolor="{#bgcolor#}"> <body bgcolor="{#bgcolor#}">
]]> ]]>
</programlisting> </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> </para>
&designers.language-variables.language-assigned-variables; &designers.language-variables.language-assigned-variables;
&designers.language-variables.language-config-variables; &designers.language-variables.language-config-variables;
&designers.language-variables.language-variables-smarty; &designers.language-variables.language-variables-smarty;

View File

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

View File

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

View File

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