王志广的个人博客

        王志广的个人博客 >> PHP >> CI整合Smarty模板

CI整合Smarty模板

admin发表于2016-04-20  880次浏览 标签: 

CI整合Smarty模板步骤如下

1、首先下载Smarty模板放到CI下的libraries目录下,修改名称为smarty目录

2、在libraries下建立Cismarty.php,并写入一下代码
<?php
if(!defined(‘BASEPATH’)) EXIT(‘No direct script asscess allowed’);
require_once( APPPATH . ‘libraries/smarty/libs/Smarty.class.php’ );
class Cismarty extends Smarty {
protected $ci;
public function __construct(){
$this->ci = & get_instance();
$this->ci->load->config(‘smarty’);//加载smarty的配置文件
//获取相关的配置项
$this->template_dir = $this->ci->config->item(‘template_dir’);
$this->complie_dir = $this->ci->config->item(‘compile_dir’);
$this->cache_dir = $this->ci->config->item(‘cache_dir’);
$this->config_dir = $this->ci->config->item(‘config_dir’);
$this->template_ext = $this->ci->config->item(‘template_ext’);
$this->caching = $this->ci->config->item(‘caching’);
$this->cache_lifetime = $this->ci->config->item(‘lefttime’);
}
}

3、在CI根目录config文件内创建smarty.php文件
<?php
if (! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
$config[‘theme’] = ‘default’;
$config[‘cache_lifetime’] = 60;
$config[‘caching’] = false;
$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、在CI目录views下建立相应的文件,地址可变

5、在CI目录config下,autoload.php 文件中,在libraries自动加载添加Cismarty
$autoload[‘libraries’] = array(‘Cismarty’);

6、在CI目录core下,创建Smarty_Controller.php
<?php if (!defined(‘BASEPATH’)) exit(‘No direct access allowed.’);
class Smarty_Controller extends CI_Controller { // 原文这里写错
public function __construct() {
parent::__construct();
}
public function assign($key,$val) {
$this->cismarty->assign($key,$val);
}
public function display($html) {
$this->cismarty->display($html);
}
}

7、使用在controllers文件中,把class类继承Smarty_Controller即可
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
class Welcome extends Smarty_Controller {
public function index() {
$this->assign(‘abc’,’hello’);
$this->display(‘test.html’);
}
}

你可以发表评论引用到你的网站或博客,或通过RSS 2.0订阅这个日志的所有评论。
上一篇:
下一篇:
没有评论
我来说两句

  Ctrl+Enter