入住 CI 中国社区 登录
CodeIgniter 中国开发者社区 返回首页

一路狂奔的个人空间 https://codeigniter.org.cn/forums/?26689 [收藏] [复制] [分享] [RSS]

日志

ci 2.1.4 + smarty 3.1.15 完美配置成功

已有 1057 次阅读2013-12-19 11:32

这两天看了不少 ci与smarty 的整合可是最新稳定版整合!特意整理了

http://my.oschina.net/observer/blog/185291

ci 2.1.4 + smarty 3.1.15 配置成功

一、准备文档下载

CodeIgniter_2.1.4点击下载、Smarty-3.1.15 点击下载

二、将Smarty-3.1.15源码包里面的libs文件夹copy到ci的项目目录application下面的libraries文件夹下,并重命名为Smarty-3.1.15

三、application下面的libraries文件夹下,创建文件 Ci_smarty.php

 

01<?php
02if(!defined('BASEPATH')) exit('No direct script access allowed');
03require(APPPATH.'libraries/Smarty-3.1.15/Smarty.class.php');
04class Ci_smarty extends Smarty
05{
06    protected $ci;
07     
08    public function __construct()
09    {
10        parent::__construct();
11         
12        $this->ci = & get_instance();
13        $this->ci->load->config('smarty');//加载smarty的配置文件
14        //获取相关的配置项
15        $this->cache_lifetime  = $this->ci->config->item('cache_lifetime');
16        $this->caching          = $this->ci->config->item('caching');
17        $this->template_dir    = $this->ci->config->item('template_dir');
18        $this->compile_dir     = $this->ci->config->item('compile_dir');
19        $this->cache_dir       = $this->ci->config->item('cache_dir');
20        $this->use_sub_dirs    = $this->ci->config->item('use_sub_dirs');
21        $this->left_delimiter  = $this->ci->config->item('left_delimiter');
22        $this->right_delimiter = $this->ci->config->item('right_delimiter');
23    }
24}


四、在config文件下创建smarty.php,copy 以下

01<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
02 
03$config['cache_lifetime']  =  30*24*3600; //更新周期
04$config['caching']             =  false;//是否使用缓存,项目在调试期间,不建议启用缓存
05$config['template_dir']     =  APPPATH.'views'; //设置模板目录
06$config['compile_dir']         =  APPPATH.'views/template_c'; //设置编译目录
07$config['cache_dir']       =  APPPATH.'views/cache';//缓存文件夹
08$config['use_sub_dirs']    =  false;   //子目录变量(是否在缓存文件夹中生成子目录)
09$config['left_delimiter']  =  '<{';
10$config['right_delimiter']     =  '}>';

五、application->config找到autoload.php,修改如下

1$autoload['libraries'] = array('Cismarty');

六、在application->core下,新建MY_Controller.php,copy 一下

01<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
02class MY_Controller extends CI_Controller { 
03    public function __construct() {
04        parent::__construct();
05    }
06     
07    public function assign($key,$val) {
08        $this->ci_smarty->assign($key,$val);
09    }
10 
11    public function display($html) {
12        $this->ci_smarty->display($html);
13    }
14}

到此已经配置成功

下面进行测试

在application->controllers目录下新建welcome.php

01<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
02class Welcome extends MY_Controller 
03{
04    public function index()
05    {
06        $test='ci 2.1.4 + smarty 3.1.15 配置成功';
07        $this->assign('test',$test);
08        $this->display('index.html');
09    }
10}

在application->views目录下新建index.html

01<!DOCTYPE html>
03<head>
04<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
05    <title>smarty配置测试</title>
06</head>
07<body>
08    <{$test}>
09</body>
10</html>

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 入住 CI 中国社区