ThinkPHP框架使用Smarty模板引擎

来源:互联网 发布:网络工作人员工资待遇 编辑:程序博客网 时间:2024/05/22 22:24

最近公司使用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 {
    publicfunctionindex(){
        $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$testas $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
原创粉丝点击