Get Smarty


Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap glasses from and save up to 80%.

Where to buy prescription glasses? is your best choice!

Where to buy discount wedding dresses and cheap smart dresses free shipping -

Shop for 2018 World Cup Football Boots Online sale at

Browse best 2018 world cup soccer cleats and shoes from



{eval}可以执行变量显示。它可用于执行模板变量中的标签/变量, 或者来自于配置文件的标签/变量。

如果你提供了assign 属性, {eval}函数的输出将不会显示,而是赋值给模板变量。

参数名称 类型 必选参数 默认值 说明
var mixed Yes n/a 需要执行的变量或字符串
assign string No n/a 用于赋值的变量名


  • 执行变量的方式和执行模板一样,使用与模板相同的解析方式和安全机制。

  • 执行变量的操作将在每次编译的时候都会启动,编译版本的程序不会被保存! 然而如果你开启了缓存设置,那么 模板的其余部分会被缓存。

  • 如果执行的内容不会经常变化,或者被多次使用,可以考虑使用 {include file="string:{$template_code}"}来代替。 这可以在编译后被缓存(速度更快),而且也不会每次编译都执行。

Example 8.3. {eval}


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#}.



{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 <strong>city</strong>.
You must supply a <strong>state</strong>.


Example 8.4. 另一个{eval}例子

例子将显示服务器名称(大写字母)和IP。 赋值的$str可以是来自于数据库的查询。

$str = 'The server name is {$smarty.server.SERVER_NAME|upper} '
       .'at {$smarty.server.SERVER_ADDR}';


    {eval var=$foo}

No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Email: (not shown)
What is 17 plus 17? (Are you human?)