php ci 模板引擎,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 文件,代码如下:

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 文件,代码如下:

$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 文件,代码如下:

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 方法如下:

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 文件, 代码如下:

smarty结合ci使用测试

{$data}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值