Codeigniter整合smarty

smarty的模板机制很强大,一般情况下CI无需整合其他模板标签,因为PHP本身就是一种标签,简单易用。codeigniter整合smarty教程(我用的都是最新版本)如下:
第一步:下载codeigniter最新版本: http://codeigniter.org.cn/downloads
第二步:下载smarty最新版本: http://www.smarty.net/download
第三步:
配置步骤:
(1)将s marty拷贝到application/libraries下,然后再根目录下下新建templates,templates_c,config,cache目录,结构如下:


codeigniter轻松整合smarty



(2)入口文件新增:define('ROOT', dirname(__FILE__));
(3)libraries下新建CI_Smarty.php


  
  
  1. <?php defined('BASEPATH') or die('Access restricted!');
  2.  
  3. require(APPPATH . 'libraries/smarty/Smarty.class.php');
  4.  
  5. class CI_Smarty extends Smarty {
  6.  
  7. public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '') {
  8. parent::__construct();
  9. if (is_array($template_dir)) {
  10. foreach ($template_dir as $key => $value) {
  11. $this->$key = $value;
  12. }
  13. } else {
  14. //ROOT是Codeigniter在入口文件index.php定义的本web应用的根目录
  15. $this->template_dir = $template_dir ? $template_dir : ROOT . '/templates';
  16. $this->compile_dir = $compile_dir ? $compile_dir : ROOT . '/templates_c';
  17. $this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
  18. $this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';
  19. }
  20. }
  21.  
  22. }

controller中使用:

  
  
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. // by www.phpddt.com
  3. class Welcome extends CI_Controller {
  4.  
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->library('CI_Smarty');
  9. }
  10. public function test()
  11. {
  12. $this->ci_smarty->assign('test', 'smarty');
  13. $this->ci_smarty->display('test.tpl');
  14. }
  15. }
  16.  
  17. /* End of file welcome.php */
  18. /* Location: ./application/controllers/welcome.php */

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值