最近公司使用ThinkPHP框架,所以比较关注,想到之前公司使用的框架用的模板引擎是 Smarty,而且用的还挺顺手的。
转到使用ThinkPHP自带的模板引擎还有点不习惯,所以在想换成Smarty模板引擎,网上看了一下,结果还是比较简单。
以此记录一下
首先ThinkPHP框架里面要有Smarty扩展 位置在 ThinkPHP\Extend\Vendor\Smarty ,如果没有就去 Smarty官网下一个最新版吧,也推荐使用最新版的。一般完整版的ThinkPHP框架都含有 Smarty扩展的。
然后只需修改配置文件 Conf\config.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?phpreturn
array( //'配置项'=>'配置值' 'TMPL_ENGINE_TYPE' =>
'Smarty', 'TMPL_ENGINE_CONFIG' =>
array( 'caching'=> TRUE, 'template_dir'=> TMPL_PATH, 'compile_dir'=> TEMP_PATH, 'cache_dir'=> CACHE_PATH, 'left_delimiter'=>
'{', 'right_delimiter'=>
'}', ),);?> |
Action:
|
1
2
3
4
5
6
7
8
9
10
|
<?phpclass
IndexAction extends
Action { publicfunction
index(){ $data=
array( 'asdf'=> 1,'dfg'
=> 2,'asdfrg'
=> 3,'yhnfd'=> 4,'bfws'
=> 1 ); $this->assign('test',$data); $this->display(); }} |
html:
|
1
2
3
4
5
|
{$smarty.now}<br />{foreach$test
as $key=>$data}{$key}:{$data}<br />{/foreach} |
最后输出:
1411459827
asdf:1
dfg:2
asdfrg:3
yhnfd:4
bfws:1
yes,这样就搞定了,使用Smarty模板就这么简单
但在thinkphp3.2.3中只需要一下配置即可:
'TMPL_ENGINE_TYPE'=>'Smarty',
'TMPL_ENGINE_CONFIG'=>array(
'plugins_dir'=>'./Application/Smarty/Plugins/'
)
转自:http://phpquan.com/lamp/php/thinkphp-smarty/?utm_source=tuicool
本文介绍如何将ThinkPHP框架从默认模板引擎转换为Smarty模板引擎的步骤和配置方法,包括配置文件修改、Action类使用及HTML模板语法变更。同时,对比了ThinkPHP3.2.3中简化配置的实现方式。

3274

被折叠的 条评论
为什么被折叠?



