What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
{include_php}
是废弃的函数,用注册插件的方式来将程序代码和显示分离,是更好的选择。
在Smarty 3.1,{include_php}
仅在SmartyBC中可用.
参数名称 | 类型 | 必选参数 | 默认值 | 说明 |
---|---|---|---|---|
file | string | Yes | n/a | 载入PHP文件的绝对路径 |
once | boolean | No | TRUE |
是否仅载入一次该PHP文件,即使多次执行。 |
assign | string | No | n/a | 将包含文件的输出赋值给变量 |
可选标记:
名称 | 说明 |
---|---|
nocache | 关闭包含PHP文件的缓存 |
{include_php}
用于载入一个PHP文件到模板中。
属性中的file
路径可以是绝对路径,或是相对于
$trusted_dir
的路径。
如果安全机制开启,那么只有在$trusted_dir
目录中的文件才可以被载入。
参见安全机制。
默认情况下,该PHP文件仅会载入一次,即使多次执行模板。
你可以设置once
属性使其每次执行都载入。
当once设置为FALSE
,PHP文件将在每次模板执行的时候都会被载入。
你可以设置可选的属性assign
,来将{include_php}
载入的内容
赋值给一个指定的模板变量,而不是输出。
在载入的PHP文件中,可以通过$_smarty_tpl->smarty
来获取到当前的Smarty对象。
Example 7.55. {include_php} 函数
load_nav.php
文件:
<?php // load in variables from a mysql db and assign them to the template require_once('database.class.php'); $db = new Db(); $db->query('select url, name from navigation order by name'); $this->assign('navigation', $db->getRows()); ?>
模板:
{* absolute path, or relative to $trusted_dir *} {include_php file='/path/to/load_nav.php'} {include_php '/path/to/load_nav.php'} {* short-hand *} {foreach item='nav' from=$navigation} <a href="{$nav.url}">{$nav.name}</a><br /> {/foreach}