mirror of
				https://github.com/smarty-php/smarty.git
				synced 2025-11-04 06:11:37 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			157 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
			
		
		
	
	
			157 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
<?xml version="1.0" encoding="iso-8859-1"?>
 | 
						|
<!-- $Revision$ -->
 | 
						|
<sect1 id="language.function.assign">
 | 
						|
 <title>{assign}</title>
 | 
						|
  <para>
 | 
						|
   {assign} is used for assigning template variables
 | 
						|
   <emphasis role="bold">during the execution
 | 
						|
   of a template</emphasis>.
 | 
						|
 </para>
 | 
						|
 
 | 
						|
 <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>
 | 
						|
 | 
						|
 <example>
 | 
						|
  <title>{assign}</title>
 | 
						|
  <programlisting>
 | 
						|
<![CDATA[
 | 
						|
{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>
 | 
						|
 | 
						|
<example>
 | 
						|
  <title>{assign} with some maths</title>
 | 
						|
<para>This complex example must have the variables in backticks</para>
 | 
						|
  <programlisting>
 | 
						|
<![CDATA[
 | 
						|
{assign var=running_total value=`$running_total+$some_array[loop].some_value`}
 | 
						|
]]>
 | 
						|
  </programlisting>
 | 
						|
 | 
						|
 </example>
 | 
						|
 
 | 
						|
 <example>
 | 
						|
  <title>Accessing {assign} variables from a PHP script.</title>
 | 
						|
  <para>
 | 
						|
  To access {assign} variables from the php script 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>
 | 
						|
</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
 | 
						|
--> |