CI框架(CI3.0.6)完美整合Smarty模板引擎(Smarty3.1.29)

配置

1. 下载ci框架和smarty, 创建项目目录

# /var/www 为web根目录
$ cd ~/download
$ wget https://codeload.github.com/bcit-ci/CodeIgniter/zip/3.0.6
$ wget https://codeload.github.com/smarty-php/smarty/zip/v3.1.29
$ unzip CodeIgniter-3.0.6.zip
$ unzip smarty-3.1.29.zip
$ mv CodeIgniter-3.0.6 /var/www/ci_smarty
$ mv smarty-3.1.29/libs /var/www/ci_smarty/application/libraries/smarty3.1.29

2. 在 /var/www/ci_smarty/application/libraries 目录下创建 Ci_smarty.php 文件,代码如下:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require(APPPATH.'libraries/smarty3.1.29/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');
    }
}

3. 在 /var/www/ci_smarty/application/config 目录下创建 smarty.php 文件,代码如下:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['cache_lifetime'] = 60;
$config['caching'] = true;
$config['template_dir'] = APPPATH .'views';
$config['compile_dir'] = APPPATH .'views/template_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. 在 /var/www/ci_smarty/application/core 目录下创建 MY_Controller.php 文件,代码如下:

<?php
class MY_controller extends CI_Controller
{
    public function __construct()
    {

        date_default_timezone_set("PRC");

        parent::__construct();

    }

    public function assign($key,$val)
    {
        $this->ci_smarty->assign($key,$val);
    }

    public function display($html)
    {
        $this->ci_smarty->display($html);
    }
}

此时, 所有的配置工作已经完成, 接下来看如何使用:

使用方法

1. 加载smarty类

有两种方法, 如果随时会用到, 推荐使用自动加载办法:
1). 在 ci框架的配置目录 config 下的 autoload.php 中自动加载类增加 smarty 类, 相关部分代码如下:

$autoload['libraries'] = array('ci_smarty');

如果只想在用到的时候加载, 则在对应的控制器的初始化方法中加载, 或者在对应的控制器的对应的方法中加载:
2). 直接执行 ci 的加载类方法:

$this->load->library("Ci_smarty");

2. 使用smarty

1). 在ci默认控制器 Welcome.php 中, 修改 index 方法如下:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends MY_Controller  {

    public function index()
    {
        $test='ci 3.0.6 结合 smarty 3.1.2 配置成功';
        $this->assign('data',$test);
        $this->display('test.html');
    }
}

2). 在 views 目录, 创建 test.html 文件, 代码如下:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>smarty结合ci使用测试</title>
    </head>
    <body>
    {$data}
    </body>
</html>

3). 浏览器访问: localhost/ci_smarty, 输出:
ci 3.0.6 结合 smarty 3.1.2 配置成功


notes: 运行之后, 会在views目录生成一个编译目录template_c, 如果在配置文件中开启了cache缓存, 则会生成对应的cache目录, 具体的目录, 皆可在config/smarty.php中配置

开启smarty分配数据时的 debug

1./php/phplib/ext/smarty/
2.打开默认smarty的debug. 修改当前smarty目录 Smarty.class.php; 修改 360行, public $debugging_ctrl = 'URL'; 此处默认为 "NONE";
3.需要将debug.tpl发送到 smarty目录
URL?SMARTY_DEBUG 可以打印数据

可以弹出窗口显示分配的数据,有array() 和 json 两种 格式


更改的部分托管于github, 地址为fizzday: https://github.com/fizzday/ci_smarty
参考地址: 吐蕃赞普的新浪博客
大功告成, 完美手工~~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • PHP 学习目录 ├─PHP视频教程 1 LAMP网站构建 │ ├─PHP教程 1.1.1 新版视频形式介绍│ ...
    曹渊说创业阅读 16,194评论 29 417
  • 食 我本是一枚吃货, 可一遇上妳, 就吃不下了, 因为秀色可餐。 色 我原是一个逗比, 可一遇上你, 就端起来了,...
    乡下童姥阅读 395评论 0 5
  • 记得之前苏苏说过要是想成功,你得你敢于做一些反人性的事,比如说反懒惰。 常人做事大部分都是三天打鱼两天晒网,而且是...
    诗蒙阅读 280评论 0 0