CI集成Smarty的简单实用方法
本帖最后由 feimengv 于 2014-4-21 11:45 编辑1.下载Smarty程序包放到application/librarise文件夹下面下载地址:http://www.onlinedown.net/soft/180811.htm
2.在application/librarise文件夹下面建立Smartys.php建立CI扩展类
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//调用Smarty核心文件
require_once(APPPATH."libraries/smarty/Smarty.class.php");
//创建和实例化smarty类
class Smartys extends Smarty{
//保护变量ci
protected $ci;
public function __construct()
{
//实例化父类
parent::__construct();
//引用CI的对象
$this->ci = & get_instance();
//加载smarty并且实例化smarty
$this->ci->load->config('smarty');
//配置smarty
$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->caching = $this->ci->config->item('caching');
$this->cache_lifetime = $this->ci->config->item('lefttime');
$this->left_delimiter = $this->ci->config->item('left_delimiter');
$this->right_delimiter = $this->ci->config->item('right_delimiter');
}
}
3.建立Smarty初始化定义变量文件application/config/smarty.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['theme'] = 'default';
$config['template_dir'] = APPPATH . 'views';
$config['compile_dir']= APPPATH . 'templates_c';
$config['cache_dir'] = APPPATH . 'cache';
$config['caching'] = false;
$config['lefttime'] = 60;
$config['left_delimiter'] = "<!--{";
$config['right_delimiter'] = "}-->";
4.追加扩展核心类方法assign和display方法
1.在system/core/Controller.php下面加两个方法
//smarty使用assign方法赋值
public function assign($key,$val) {
$this->smartys->assign($key,$val);
}
//smarty指定引用的模板文件
public function display($html) {
$this->smartys->display($html);
}
5.启用自动加载Smarty类
application/config/autoload.php
启用自动加载
$autoload['libraries'] = array('Smartys');
6.创建文件夹
templates_c就可以去了,其他使用CI默认的文件夹
7.在模板文件里面就可以调用了
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table border='1'>
<thead>
<tr>
<th>用户名</th>
</tr>
</thead>
<tbody>
<!--{foreach $result as $k=>$v}-->
<tr>
<td><!--{$v.username}--></td>
</tr>
<!--{/foreach}-->
</tbody>
</table>
</body>
</html>
第四改了框架
应该继承一下 道理是这个道理,但是继承以后,每次饮用类都要写成MY_***,何必不直接类下扩展一下,这样本人感觉更舒服一些!可能就是代码抒写习惯吧 :victory:
页:
[1]