This commit is contained in:
pete_morgan
2006-03-27 23:58:23 +00:00
parent e06a467a6c
commit 6b920dcdb3
6 changed files with 61 additions and 35 deletions

View File

@@ -3,11 +3,20 @@
<chapter id="language.builtin.functions"> <chapter id="language.builtin.functions">
<title>Built-in Functions</title> <title>Built-in Functions</title>
<para> <para>
Smarty comes with several built-in functions. Built-in functions Smarty comes with several built-in functions. These built-in functions
are integral to the template language. You cannot create custom are the integral part of the smarty template engine. You cannot create your own
functions with the same names, nor can you modify built-in functions. <link linkend="language.custom.functions">custom functions</linkend>
with the same name; and you should not need to
modify the built-in functions.
</para>
<para>
A few on these functions have an <emphasis role="bold">assign</emphasis>
attribute which collects
the result the function to a named variable within a template instead of being output;
much like the <link linkend="language.function.assign">{assign}</link> function.
</para> </para>
&designers.language-builtin-functions.language-function-capture; &designers.language-builtin-functions.language-function-capture;
&designers.language-builtin-functions.language-function-config-load; &designers.language-builtin-functions.language-function-config-load;
&designers.language-builtin-functions.language-function-foreach; &designers.language-builtin-functions.language-function-foreach;

View File

@@ -4,15 +4,17 @@
<title>{capture}</title> <title>{capture}</title>
<para> <para>
{capture} is used to collect the output of the template into a {capture} is used to collect the output of the template between the tags into a
variable instead of displaying it. Any content between {capture variable instead of displaying it. Any content between {capture
name="foo"} and {/capture} is collected into the variable specified name='foo'} and {/capture} is collected into the variable specified
in the name attribute. The captured content can be used in the in the name attribute.
template from the special variable <link </para>
<para>The captured content can be used in the
template from the variable <link
linkend="language.variables.smarty.capture">$smarty.capture.foo</link> linkend="language.variables.smarty.capture">$smarty.capture.foo</link>
where "foo" is the value passed in the name attribute. If you do not where "foo" is the value passed in the name attribute. If you do not
supply a name attribute, then "default" will be used as the name. All {capture} supply a name attribute, then "default" will be used as in $smarty.capture.default.
commands must be paired with {/capture}. You can nest capture commands. You can nest capture commands.
</para> </para>
@@ -66,39 +68,33 @@
</caution> </caution>
<para> <para>
<example> <example>
<title>capturing template content</title> <title>{capture} with a the name attribute</title>
<programlisting> <programlisting>
<![CDATA[ <![CDATA[
{* we don't want to print a table row unless content is displayed *} {* we don't want to print a div tag unless content is displayed *}
{capture name=banner} {capture name=banner}
{include file='get_banner.tpl'} {include file='get_banner.tpl'}
{/capture} {/capture}
{if $smarty.capture.banner ne ''} {if $smarty.capture.banner ne ''}
<table> <div id="banner">{$smarty.capture.banner}</div>
<tr>
<td>
{$smarty.capture.banner}
</td>
</tr>
</table>
{/if} {/if}
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<example> <example>
<title>capturing content to a variable</title> <title>{capture} into a template variable</title>
<para>This example also demonstrates the <para>This example also demonstrates the
<link linkend="language.function.popup">{popup}</link> <link linkend="language.function.popup">{popup}</link>
function</para> function</para>
<programlisting> <programlisting>
<![CDATA[ <![CDATA[
{capture name=some_content assign=popText} {capture name=some_content assign=popText}
.... some popup content .... The server is {$smarty.server.SERVER_NAME|upper} at {$smarty.server.SERVER_ADDR}<br>
Your ip is {$smarty.server.REMOTE_ADDR}.
{/capture} {/capture}
<a href="#" {popup caption='Server Info' text=$popText}>help</a>
<a href="#" {popup caption='Help' text=$popText}>help</a>
]]> ]]>
</programlisting> </programlisting>
</example> </example>

View File

@@ -5,7 +5,7 @@
<para> <para>
{assign} is used for assigning template variables {assign} is used for assigning template variables
<emphasis role="bold">during the execution <emphasis role="bold">during the execution
of the template</emphasis>. of a template</emphasis>.
</para> </para>
<informaltable frame="all"> <informaltable frame="all">
@@ -76,7 +76,7 @@ The value of $name is Bob.
<example> <example>
<title>Accessing {assign} variables from a PHP script.</title> <title>Accessing {assign} variables from a PHP script.</title>
<para> <para>
To access {assign} variables from php use To access {assign} variables from the php script use
<link linkend="api.get.template.vars">get_template_vars()</link>. <link linkend="api.get.template.vars">get_template_vars()</link>.
However, the variables are only available after/during template execution However, the variables are only available after/during template execution
as in the following example as in the following example

View File

@@ -45,6 +45,11 @@
{* php's shuffle on servers's ip *} {* php's shuffle on servers's ip *}
{$smarty.server.SERVER_ADDR|shuffle} {$smarty.server.SERVER_ADDR|shuffle}
(* this will uppercase and truncate the whole array *}
<select name="name_id">
{html_options output=$myArray|upper|truncate:20}
</select>
]]> ]]>
</programlisting> </programlisting>
</example> </example>

View File

@@ -7,6 +7,14 @@
is empty or unset, the given default value is printed instead. is empty or unset, the given default value is printed instead.
Default takes one argument. Default takes one argument.
</para> </para>
<para>
<note>
<para>
With error_reporting(E_ALL), undeclared variables will always throw an error within the template. This function
is useful for replacing null or zero length strings.
</para>
</note>
</para>
<informaltable frame="all"> <informaltable frame="all">
<tgroup cols="5"> <tgroup cols="5">

View File

@@ -3,9 +3,8 @@
<sect1 id="language.modifier.escape"> <sect1 id="language.modifier.escape">
<title>escape</title> <title>escape</title>
<para> <para>
This is used to html escape, url escape, escape single quotes on a This is used to encode/escape and a variable to html url single quotes, hex escape, hexentity, javascript and mail escape.
variable not already escaped, hex escape, hexentity or javascript escape. By default its html
By default, the variable is html
escaped. escaped.
</para> </para>
@@ -69,12 +68,12 @@ $smarty->assign('EmailAddress','smarty@example.com');
<![CDATA[ <![CDATA[
{$articleTitle} {$articleTitle}
{$articleTitle|escape} {$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' < > *} {$articleTitle|escape:'html'} {* escapes & " ' < > *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *} {$articleTitle|escape:'htmlall'} {* escapes ALL html entities *}
{$articleTitle|escape:"url"} {$articleTitle|escape:'url'}
{$articleTitle|escape:"quotes"} {$articleTitle|escape:'quotes'}
<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a> <a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
{$EmailAddress|escape:'mail'} {$EmailAddress|escape:'mail'} {* this converts to email to text *}
]]> ]]>
</programlisting> </programlisting>
<para> <para>
@@ -92,19 +91,28 @@ $smarty->assign('EmailAddress','smarty@example.com');
smarty [AT] example [DOT] com smarty [AT] example [DOT] com
]]> ]]>
</screen> </screen>
<para>Remember that native PHP functions can be used as modifiers so this will work</para> <para>Note that native PHP functions can be used as modifiers so this will work</para>
<screen> <screen>
<![CDATA[ <![CDATA[
{* rewind get var registers the current location *} {* rewind get var registers the current location *}
<a href="{$SCRIPT_NAME}?page=foo&rewind={$smarty.server.REQUEST_URI|urlencode}">click here</a> <a href="{$SCRIPT_NAME}?page=foo&rewind={$smarty.server.REQUEST_URI|urlencode}">click here</a>
]]> ]]>
</screen> </screen>
<para>And this is very useful for emails but see also
<link linkend="language.function.mailto">{mailto}</link></para>
<screen>
<![CDATA[
{* email address mangled *}
<a href="mailto:{$EmailAddress|escape:'hex'}">{$EmailAddress:escape'mail'}</a>
]]>
</screen>
</example> </example>
<para> <para>
See also <link linkend="language.escaping">Escaping Smarty Parsing</link> See also <link linkend="language.escaping">Escaping Smarty Parsing</link>,
<link linkend="language.function.mailto">{mailto}</link>
and <link linkend="tips.obfuscating.email">Obfuscating E-mail Addresses</link>. and <link linkend="tips.obfuscating.email">Obfuscating E-mail Addresses</link>.
</para> </para>
</sect1> </sect1>