- 论坛徽章:
- 0
|
由于这次的项目对方客户指定要求用CakePHP,所以只好硬着头皮去学习这个框架,无奈客户还有个要求,就是模板要使用Smarty,这下配置环境成了大问题,网上很多文章都是讲CakePHP 1.1和Smarty的整合的,很少有见1.2的,不过还是找到了方法,使Smarty成为CakePHP的一个组件,这样就完成了整合.
下面是整合的方法:
1.下载1.2版本的CakePHP,放在web目录中.
2.在\vendors目录下建立smarty目录
3.下载新版的Smarty,将解压后的libs目录整个拷贝到\vendors\smarty目录下
4.在\app\controllers\components\下建立新文件,文件名为smarty.php,内容如下:
?php
vendor('Smarty' . DS . 'libs' . DS . 'Smarty.class');
class SmartyComponent extends Smarty {
var $controller;
var $template_dir;
var $compile_dir;
var $cache_dir;
function __construct() {
parent::__construct();
$this->template_dir = VIEWS;
$this->compile_dir = TMP . 'smarty' . DS . 'compile' . DS;
$this->cache_dir = TMP . 'smarty' . DS . 'cache' . DS;
$this->left_delimiter = ';
$this->right_delimiter = '}>';
}
}
?>
5.在/app/tmp目录下建立smarty/cache和smarty/compile目录6.在controller中使用
var $components = array('smarty');
调用smarty,并使用
function render() {}
来防止CakePHP调用本事的view去显示页面.
下面是一个简单的controller的例子
class PostsController extends AppController {
var $name = 'Posts';
var $components = array('smarty');
function index() {
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 3600*5;
$this->smarty->assign('posts', $this->Post->findAll());
$this->smarty->display('posts/index.tpl');
}
function render() {}
}
index.tpl与普通的smarty模板没有任何区别.
如果大家在使用中有任何问题,欢迎与我讨论.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/79633/showart_1205319.html |
|