Files
smarty/docs/en/designers/language-custom-functions/language-function-assign.xml

144 lines
3.6 KiB
XML
Raw Normal View History

2004-04-13 08:46:28 +00:00
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="language.function.assign">
<title>{assign}</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Attribute Name</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>var</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The name of the variable being assigned</entry>
</row>
<row>
<entry>value</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The value being assigned</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
2005-05-27 16:25:02 +00:00
{assign} is used for assigning template variables
<emphasis role="bold">during the execution
of the template</emphasis>.
</para>
<example>
2005-05-27 16:25:02 +00:00
<title>{assign}</title>
<programlisting>
<![CDATA[
2004-04-13 08:46:28 +00:00
{assign var="name" value="Bob"}
The value of $name is {$name}.
]]>
</programlisting>
<para>
The above example will output:
</para>
<screen>
<![CDATA[
The value of $name is Bob.
]]>
</screen>
</example>
2005-05-27 16:25:02 +00:00
<example>
<title>Accessing {assign} variables from a PHP script.</title>
<para>
To access {assign} variables from php use
<link linkend="api.get.template.vars">get_template_vars()</link>.
However, the variables are only available after/during template execution
as in the following example
</para>
<programlisting>
<![CDATA[
{* index.tpl *}
{assign var="foo" value="Smarty"}
]]>
</programlisting>
<programlisting role="php">
<![CDATA[
<?php
// this will output nothing as the template has not been executed
echo $smarty->get_template_vars('foo');
// fetch the template to a dead variable
$dead = $smarty->fetch('index.tpl');
// this will output 'smarty' as the template has been executed
echo $smarty->get_template_vars('foo');
$smarty->assign('foo','Even smarter');
// this will output 'Even smarter'
echo $smarty->get_template_vars('foo');
?>
]]>
</programlisting>
</example>
<para>
The following functions can also <emphasis>optionally</emphasis> assign
template variables.
</para>
<para>
<link linkend="language.function.capture">{capture}</link>,
<link linkend="language.function.include">{include}</link>,
<link linkend="language.function.include.php">{include_php}</link>,
<link linkend="language.function.insert">{insert}</link>,
<link linkend="language.function.counter">{counter}</link>,
<link linkend="language.function.cycle">{cycle}</link>,
<link linkend="language.function.eval">{eval}</link>,
<link linkend="language.function.fetch">{fetch}</link>,
<link linkend="language.function.math">{math}</link>,
<link linkend="language.function.textformat">{textformat}</link>
</para>
<para>
See also <link linkend="api.assign">assign()</link>
and
<link linkend="api.get.template.vars">get_template_vars()</link>.
</para>
2004-04-13 08:46:28 +00:00
</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
-->