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

155 lines
3.9 KiB
XML
Raw Normal View History

2004-04-13 08:46:28 +00:00
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
2005-05-10 11:42:40 +00:00
<sect1 id="language.function.eval">
<title>{eval}</title>
2005-09-13 19:03:11 +00:00
<para>
<varname>{eval}</varname> is used to evaluate a variable as a template.
This can be used for things like embedding template tags/variables into
variables or tags/variables into config file variables.
2005-09-13 19:03:11 +00:00
</para>
<para>
If you supply the <parameter>assign</parameter> attribute, the output of the
<varname>{eval}</varname> function will be assigned to this template
variable instead of being output to the template.
2005-09-13 19:03:11 +00:00
</para>
2005-05-10 11:42:40 +00:00
<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>mixed</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>Variable (or string) to evaluate</entry>
2005-05-10 11:42:40 +00:00
</row>
<row>
<entry>assign</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The template variable the output will be assigned
2005-05-10 11:42:40 +00:00
to</entry>
</row>
</tbody>
</tgroup>
</informaltable>
2005-09-13 19:03:11 +00:00
2005-05-10 11:42:40 +00:00
<note>
<title>Technical Note</title>
<para>
<itemizedlist>
<listitem><para>
2005-05-10 11:42:40 +00:00
Evaluated variables are treated the same as templates. They follow
the same escapement and security features just as if they were
templates.
</para></listitem>
<listitem><para>
2005-05-10 11:42:40 +00:00
Evaluated variables are compiled on every invocation, the compiled
versions are not saved! However if you have
<link linkend="caching">caching</link> enabled, the
2005-05-10 11:42:40 +00:00
output will be cached with the rest of the template.
</para></listitem>
</itemizedlist>
2005-05-10 11:42:40 +00:00
</para>
</note>
2005-05-10 11:42:40 +00:00
<example>
<title>{eval}</title>
<para>The contents of the config file, <filename>setup.conf</filename>.</para>
2005-05-10 11:42:40 +00:00
<programlisting>
<![CDATA[
2006-03-26 14:59:10 +00:00
emphstart = <strong>
emphend = </strong>
title = Welcome to {$company}'s home page!
ErrorCity = You must supply a {#emphstart#}city{#emphend#}.
ErrorState = You must supply a {#emphstart#}state{#emphend#}.
2005-05-10 11:42:40 +00:00
]]>
</programlisting>
<para>
2006-03-26 14:59:10 +00:00
Where the template is:
2005-05-10 11:42:40 +00:00
</para>
<programlisting>
<![CDATA[
2006-03-26 14:59:10 +00:00
{config_load file='setup.conf'}
2004-04-13 08:46:28 +00:00
2006-03-26 14:59:10 +00:00
{eval var=$foo}
{eval var=#title#}
{eval var=#ErrorCity#}
{eval var=#ErrorState# assign='state_error'}
{$state_error}
2005-05-10 11:42:40 +00:00
]]>
</programlisting>
<para>
The above template will output:
2005-05-10 11:42:40 +00:00
</para>
<screen>
<![CDATA[
2006-03-26 14:59:10 +00:00
This is the contents of foo.
Welcome to Foobar Pub & Grill's home page!
You must supply a <strong>city</strong>.
You must supply a <strong>state</strong>.
2005-05-10 11:42:40 +00:00
]]>
</screen>
</example>
2006-03-26 14:59:10 +00:00
<example>
<title>Another {eval} example</title>
<para>This outputs the server name (in uppercase) and IP. The assigned
variable <parameter>$str</parameter> could be from a database query.</para>
2006-03-26 14:59:10 +00:00
<programlisting role="php">
<![CDATA[
<?php
2006-03-26 14:59:10 +00:00
$str = 'The server name is {$smarty.server.SERVER_NAME|upper} '
2006-03-26 15:04:50 +00:00
.'at {$smarty.server.SERVER_ADDR}';
2006-03-26 14:59:10 +00:00
$smarty->assign('foo',$str);
?>
2006-03-26 14:59:10 +00:00
]]>
</programlisting>
<para>
Where the template is:
</para>
<programlisting>
<![CDATA[
{eval var=$foo}
]]>
</programlisting>
</example>
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
-->