include_php Smarty

博客介绍了Smarty中include_php标签的使用。该标签基本已被弃用,可通过自定义模板函数实现相同功能。它用于在模板中包含PHP脚本,有安全和属性要求,能处理组件化模板,还可设置包含次数、传递assign属性等,并给出了使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Technical Note: include_php is pretty much deprecated from Smarty, you can accomplish the same functionality
via a custom template function. The only reason to use include_php is if you really have a need to quarantine the
php function away from the plugin directory or your application code. See the componentized template example
for details.
include_php tags are used to include a php script in your template. If security is enabled, then the php script must be located
in the $trusted_dir path. The include_php tag must have the attribute "file", which contains the path to the included php file,
either relative to $trusted_dir, or an absolute path.
include_php is a nice way to handle componentized templates, and keep PHP code separate from the template files. Lets say
you have a template that shows your site navigation, which is pulled dynamically from a database. You can keep your PHP
logic that grabs database content in a separate directory, and include it at the top of the template. Now you can include this
template anywhere without worrying if the database information was assigned by the application before hand.
By default, php files are only included once even if called multiple times in the template. You can specify that it should be
included every time with the once attribute. Setting once to false will include the php script each time it is included in the
template.
You can optionally pass the assign attribute, which will specify a template variable name that the output of include_php will
be assigned to instead of displayed.
The smarty object is available as $this within the PHP script that you include.
Example 7.9. function include_php  
load_nav.php
-------------
<?php
// load in variables from a mysql db and assign them to the template
require_once("MySQL.class.php");
$sql = new MySQL;
$sql->query("select * from site_nav_sections order by name",SQL_ALL);
$this->assign('sections',$sql->record);
?>
index.tpl
---------
{* absolute path, or relative to $trusted_dir *}
{include_php file="/path/to/load_nav.php"}
{foreach item="curr_section" from=$sections}
<a href="{$curr_section.url}">{$curr_section.name}</a><br>
{/foreach}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值