Attribute Name Type Required Default Description
var mixed Yes n/a variable (or string) to evaluate
assign string No n/a the template variable the output will be assigned to

属性 类型 是否必须 缺省值 描述
var mixed Yes n/a 待求值的变量(或字符串)
assign string No n/a 输出值将被赋给模板变量的名称

eval 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.

eval 按处理模板的方式计算取得变量的值. 该特性可用于在配置文件中的标签/变量中嵌入其它模板标签/变量.

If you supply the special "assign" attribute, the output of the eval function will be assigned to this template variable instead of being output to the template.

如果指定了 "assign" 这个特殊属性,该函数的输出值将被赋给由 assign 指定的模板变量,而不是直接输出.

Technical Note: Evaluated variables are treated the same as templates. They follow the same escapement and security features just as if they were templates.

技术要点: 待求值处理的变量被当作模板来处理. 它们和模板一样遵循同样的结构和安全特性.

Technical Note: Evaluated variables are compiled on every invocation, the compiled versions are not saved! However if you have caching enabled, the output will be cached with the rest of the template.

技术要点: 待求值处理的变量每次调用时被重编译,不保存编译版本! 但当打开缓冲设置时,该输出会被其它模板缓冲.

Example 8-4. eval
Smarty手册范例 8-4. eval 函数演示


emphstart = <b>
emphend = </b>
title = Welcome to {$company}'s home page!
ErrorCity = You must supply a {#emphstart#}city{#emphend#}.
ErrorState = You must supply a {#emphstart#}state{#emphend#}.


{config_load file="setup.conf"}

{eval var=$foo}
{eval var=#title#}
{eval var=#ErrorCity#}
{eval var=#ErrorState# assign="state_error"}


This is the contents of foo.
Welcome to Foobar Pub & Grill's home page!
You must supply a <b>city</b>.
You must supply a <b>state</b>.