免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2957 | 回复: 0
打印 上一主题 下一主题

在CakePHP中使用Smarty作为模板引擎 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-18 14:08 |只看该作者 |倒序浏览
    由于这次的项目对方客户指定要求用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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP