用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4225|回复: 3
收起左侧

[其它 Other] ci 2.1.4 + smarty 3.1.15 完美整合配置成功

[复制链接]
发表于 2013-12-20 09:14:06 | 显示全部楼层 |阅读模式
这两天看了不少 ci与smarty 的整合可是最新稳定版整合!特意整理了
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
PHP复制代码
 
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH.'libraries/Smarty-3.1.15/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->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');
    }
}
 
复制代码

四、在config文件下创建smarty.php,copy 以下
PHP复制代码
 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
$config['cache_lifetime']     =     30*24*3600; //更新周期
$config['caching']             =     false;//是否使用缓存,项目在调试期间,不建议启用缓存
$config['template_dir']        =     APPPATH.'views'; //设置模板目录
$config['compile_dir']         =     APPPATH.'views/template_c'; //设置编译目录
$config['cache_dir']         =     APPPATH.'views/cache';//缓存文件夹
$config['use_sub_dirs']     =     true;   //子目录变量(是否在缓存文件夹中生成子目录)
$config['left_delimiter']     =     '<{';
$config['right_delimiter']     =     '}>';
 
复制代码

五、application->config找到autoload.php,修改如下
PHP复制代码
 
$autoload['libraries'] = array('Cismarty');
 
复制代码

六、在application->core下,新建MY_Controller.php,copy 一下
PHP复制代码
 
<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
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);
    }
}
 
复制代码


到此已经配置成功
下面进行测试
在application->controllers目录下新建welcome.php
PHP复制代码
 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller
{
    public function index()
    {
        $test='ci 2.1.4 + smarty 3.1.15 配置成功';
        $this->assign('test',$test);
        $this->display('index.html');
    }
}
 
复制代码


在application->views目录下新建index.html
HTML复制代码
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>smarty配置测试</title>
</head>
<body>
    <{$test}>
</body>
</html>
 
复制代码

正在用这个做一个cms 做好了会放出来
来自:http://blog.newpopline.com/web/ci-2-1-4-smarty-3-1-15.html




发表于 2013-12-20 09:38:09 | 显示全部楼层
建议改进:你的Ci_smarty.php添加如下:
PHP复制代码
 
[font=Courier New]if(function_exists([/font]'site_url')) { // URL  helper required
$this->assign('site_url',  site_url()); // 因为页面通常会用到site_url        
}
//下边2个是ci的系统变量,建议加上
$this->assign('elapsed_time', $this->CI->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end'));
$this->assign('memory_usage',  ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2) . 'MB');
 
复制代码


我是这样集成的,和你差不多,呵呵。
http://hi.baidu.com/michaelzhouh/item/faf172009b519b92a3df4315

 楼主| 发表于 2013-12-20 10:12:46 | 显示全部楼层
建议不错
site_url 这个方法页面 加载的时候url autoload $autoload['helper'] = array('url');
模版里面就可以直接使用
发表于 2014-9-3 16:54:36 | 显示全部楼层
Fatal error: Call to a member function createTemplate() on a non-object in E:\GzZpjWork\WWW\CICMS\application\libraries\Smarty-3.1.19\sysplugins\smarty_internal_templatebase.php on line 49

请问楼主这个怎么解决!

本版积分规则