ThinkPHP框架使用Smarty模板引擎

最近公司使用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
<?php
return 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
<?php
class IndexAction extends Action {
     public function 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值