对比了内置的模板引擎和tp 编写的smarty驱动,
发现差别在于,tp内置的模板引擎编译模板文件前,会监听字符替换行为,即tp内置的模板默认会加载 ContentReplaceBehavior.class.php,
而tp的smarty驱动中,并没有关于相关的行为加载,
所以,只好在ThinkPHP/Library/Think/View.class.php 输出模板内容之前,加上字符替换行为,即在:第130附近的fetch() 方法中修改:
修改前:
// 获取并清空缓存
$content = ob_get_clean();
// 内容过滤标签
Hook::listen('view_filter',$content);
// 输出模板文件
return $content;
修改后:
// 获取并清空缓存
$content = ob_get_clean();
// 内容过滤标签
Hook::listen('view_filter',$content);
//原来的tp3.2.3使用smarty3.1.29后,不替换模板变量,加上一句,行为操作,就可以了。我的修改,2016-06-09
Hook::listen('template_filter',$content);
// 输出模板文件
return $content;
这样就可以让smarty输出 __ROOT__ 、 __URL__ 等常量了。
原理:
tp默认的模板引擎,会使用到ThinkPHP/mode/common.php中的配置项,
并且在tp默认的模板引擎ThinkPHP/Library/Think/Template.class.php
中的compliter()方法中的第130行:Hook::listen('template_filter', $tmplcontent);
调取ThinkPHP/mode/common.php中的template_filter行为,即:执行Behavior\ContentReplaceBehavior,而Behavior\ContentReplaceBehavior就是
ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php文件,在这个文件中有相关的字符替换。
但tp给的smarty模板驱动是ThinkPHP/Library/Think/Template/Drive/Smarty.class.php文件,而在这个文件中,只是简单的提供了将要输出的变量smarty模板引擎,并没有其他的操作。但最后输出和提交给smarty处理的程序是是 ThinkPHP/Library/Think/View.class.php。 所以,就在smarty编译数据之前,在view中先替换字符变量。
这样一来,就可以让smarty使用tp的部分内置变量了。
最佳答案