codeigniter3整合smarty3

简介:
  1. 切换到ci路径下

  2. 在application/libraries创建smarty文件夹,并将解压好的Smarty库中的libs文件夹复制到Smarty文件夹中

  3. 在application/config下创建smarty.php, 代码如下:

    <?php
    if (!defined('BASEPATH')) {
        exit('No direct script access allowed');
    }
    $config['cache_lifetime'] = 60;
    $config['caching'] = false;
    $config['template_dir'] = APPPATH . 'views/templates';
    $config['compile_dir'] = APPPATH . 'views/templates_c';
    $config['cache_dir'] = APPPATH . 'views/cache';
    $config['config_dir'] = APPPATH . 'views/config';
    $config['use_sub_dirs'] = false;
    //子目录变量(是否在缓存文件夹中生成子目录)
    $config['left_delimiter'] = '{';
    $config['right_delimiter'] = '}';

  4. 在application/libraries下创建一个Ci_Smarty.php,代码如下

    <?php
    if (!defined('BASEPATH')) {
        exit('No direct script access allowed');
    }
    require_once APPPATH.'libraries/smarty/libs/Smarty.class.php';
    class Ci_Smarty extends Smarty
    {
        protected $ci;
        public function __construct()
        {
            parent::__construct();
            $this -> ci =& get_instance();
            $this -> ci -> load -> config('smarty');
            //加载smarty的配置文件
            $this -> cache_lifetime = $this -> ci -> config -> item('cache_lifetime');
            $this -> caching = $this -> ci -> config -> item('caching');
            $this -> config_dir = $this -> ci -> config -> item('config_dir');
            $this -> template_dir = $this -> ci -> config -> item('template_dir');
            $this -> compile_dir = $this -> ci -> config -> item('compile_dir');
            $this -> cache_dir = $this -> ci -> config -> item('cache_dir');
            $this -> use_sub_dirs = $this -> ci -> config -> item('use_sub_dirs');
            $this -> left_delimiter = $this -> ci -> config -> item('left_delimiter');
            $this -> right_delimiter = $this -> ci -> config -> item('right_delimiter');
        }
    }

  5. 打开application/config/autoload.php 修改

    $autoload['libraries'] = array(''); 
    改为
    $autoload['libraries'] = array('Ci_Smarty');

  6. application/core下新建一个MY_Controller.php ,代码如下:

    <?php
    class MY_controller extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function assign($key, $val)
        {
            $this->ci_smarty->assign($key, $val);
        }
        public function display($html)
        {
            $this->ci_smarty->display($html);
        }
    }

  7. 在views下创建templates目录

  8. 在templates下新建一个测试模板test.tpl

    hello.world

  9. 在controllers下新建一个测试文件Pages.php继承MY_controller

    <?php
    class Pages extends MY_controller {
        public function index()
        {
            $this->display('test.tpl');
        }
    }

  10. wKiom1lbQu6iEPTvAAAoA7665nE783.jpg-wh_50

  11. 至此完成










本文转自 无心低语 51CTO博客,原文链接:http://blog.51cto.com/fengzhankui/1944428,如需转载请自行联系原作者

目录
相关文章
|
PHP 网络架构 前端开发
|
PHP 前端开发 网络架构
|
编解码 JavaScript 前端开发
|
PHP 搜索推荐 数据库
|
PHP 前端开发
**【ci框架】PHP的CI框架集成Smarty的最佳方式
因为CI自带的模板功能不是很方便,所以大家普遍采用集成Smarty的方式来弥补CI这方面的不足。 本人在网上看了不少CI集成Smarty的教程,包括咱们CI论坛里面的一个精华帖子 http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=10345。
1029 0
|
PHP Apache 缓存
yaf(5) smarty
2013年4月6日 13:41:37 参考: http://www.oschina.net/question/812776_71817 http://yaf.laruence.com/manual/yaf.
876 0
|
Web App开发 缓存 程序员
smarty
引用:http://www.baike.com/wiki/smarty Smarty是一个使用PHP写出来的模板PHP模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。
1129 0

热门文章

最新文章