codeigniter集成smarty

参考文章http://www.cnblogs.com/breakdown/archive/2012/10/25/2739011.html

首先,将smarty的libs目录放在application/libraries目录下,改名为smarty。目录结构如下:

 其次,在application/libraries下创建cismarty类文件,内容为:

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

require(APPPATH . 'libraries/smarty/Smarty.class.php');

class Cismarty extends Smarty
{
    public function __construct()
    {

        parent::__construct();
        $this->caching = false;
        $this->setTemplateDir(APPPATH . 'views/Smarty/templates');
        $this->setConfigDir(APPPATH . 'views/Smarty/configs'); 
        $this->setCacheDir(APPPATH . 'views/Smarty/cache'); 
        $this->setPluginsDir(APPPATH . 'views/Smarty/plugins'); 
        $this->setCompileDir(APPPATH . 'views/Smarty/templates_c'); 
    }
}
 

 

 然后,在application/views下创建smarty目录,在此目录中创建文件夹cache、configs、templates、templates_c。

这样就可以在控制器中使用smarty了。举例说明,创建welcome控制器:

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

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->library('cismarty');
        $this->cismarty->assign("name", 'test name');
        $this->cismarty->display('test.tpl');
    }
}

 

在views/smarty/templates中创建test.tpl,内容为:

hello world {$name}

 

访问welcome控制器,便可以看到$name被解析为test name了。

 


 

posted @ 2015-02-07 14:41  yikai.shao  阅读(158)  评论(1编辑  收藏  举报