smarty 包含php文件,php,smarty:检查不同文件夹中的模板

本文讨论了一个在前端应用中,如何通过代码实现从/ext/templates目录的优先查找,如果资源不可用则回退到/base/templates的巧妙设计。展示了__default_template_handler函数的实现细节,确保了模板资源的可靠加载。
摘要由CSDN通过智能技术生成

我知道问这个问题已经有一段时间了,但为了记录,我想指出一个聪明的特点,我认为这是行动要求的。下面是我的应用程序中的一部分代码。

基本上它所做的就是首先在

/ext/templates

. 如果在那里找不到它,它将使用来自

/base/templates

. 我的代码只需要一个回退,但更容易添加。

class View extends Smarty {

function __construct() {

parent::Smarty();

$this->template_dir = LOCAL_APP_ROOT.'/ext/templates';

$this->compile_dir = LOCAL_APP_ROOT.'/cache/compile';

$this->default_template_handler_func = '__default_template_handler';

}

}

function __default_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj) {

if ($resource_type == 'file') {

if (!is_readable($resource_name)) {

$defaultPath = LOCAL_APP_ROOT."/base/templates/$resource_name";

if (file_exists($defaultPath)) {

$template_source = file_get_contents($defaultPath);

$template_timestamp = filemtime($defaultPath);

return true;

}

}

}

return false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值