CodeIgniter3集成Smarty
先吐槽一下,CI框架对第三方类库的扩展其实并不是很好用,我觉得最主要的原因就是CI没有引入PHP的命名空间,引入第三方类库的时候很容易就发生命名冲突的事。当然这也是CI的一大特点——轻。在CI3发布的时候说是进行了重构,但是我自己对比了一下,结构完全没有变,文档基本没有变,有些类工作流程和代码也基本是一样的,也没有使用namespace,最终就是修改了一大堆bug和一些细节处的功能增加,还是有点失望。其实我并没有通读过CI的代码也不够深入框架,上面的看法也许非常粗浅,希望有大神提出深入的看法。
正文
CI中的第三方类库是放到/application/third_party文件夹下面。这里CI官方文档一笔带过没有相关的规范,那我们就直接解压我下载的smarty-3.1.24.zip到上面这个文件夹。这样smarty的目录就在/application/third_party/smarty-3.1.24,Smarty.class.php文件存在于/application/third_party/smarty-3.1.24/libs。
接下来使用自建类使用Smarty:在/application/libraries目录下创建Use_smarty类。由于不支持命名空间,不能命名为Smarty,否则跟Smarty类命名冲突。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * Smarty Class * * CodeIgniter3下Smarty使用类。 * * 参考Kepler Gelotte版本(http://www.coolphptools.com/codeigniter-smarty) * * @package CodeIgniter * @subpackage Libraries * @category Smarty * @author mcxr4299@gmail.com */ require_once( APPPATH.'third_party/smarty-3.1.24/libs/Smarty.class.php' ); class Use_smarty extends Smarty { function __construct() { parent::__construct(); $this->compile_dir = APPPATH . "views/temp"; $this->template_dir = APPPATH . "views"; $this->assign( 'APPPATH', APPPATH ); $this->assign( 'BASEPATH', BASEPATH ); // Assign CodeIgniter object by reference to CI if ( method_exists( $this, 'assignByRef') ) { $CI =& get_instance(); $this->assignByRef("CI", $CI); } } /** * 使用Smarty引擎解析模板 * * 将smarty的assign()和display()一步完成。 * $data参数同ci的视图加载函数,是键值对数组。 * * @access public * @param string $template 模板路径 * @param array $data 输出变量 * @param boolean $return 是否返回解析结果 返回结果则不输出到浏览器 * @return mix string解析结果/不返回 */ public function view($template, $data = array(), $return = FALSE) { $file = $template . '.php'; // 沿用ci的视图后缀.php foreach ($data as $key => $val) { $this->assign($key, $val); } if (! $return) { $CI =& get_instance(); $CI->output->append_output( $this->fetch($file) ); return; } else { return $this->fetch($file); } } } // END Smarty Class |
还需要一个步骤,自动加载这个类。修改/application/config/autoload.php文件:
1 |
$autoload['libraries'] = array('use_smarty' => 'smarty'); |
这里为了调用方便,把use_smarty重命名为了smarty,这样就可以在控制器中使用$this->smarty而不是$this->use_smarty。当然也可以不自动加载类,使用 $this->load->library('use_smarty'); 加载类,前面就要使用后者来调用此类了。
接下来就是使用测试这个类。
新建模板文件/application/views/common/test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<h1>Hello Smarty!</h1> <p>输出变量:{$hello}</p> <p>使用函数:{base_url('js/jquery.js')}</p> <p>输出常量:{$smarty.const.APPPATH}</p> <p>数学运算:{2*3+4}</p> <p>并不能执行原php标签(Ctrl+U):<?php echo 'php';?></p> 循环: <ul> {foreach $array1 as $value} <li>{$value}</li> {/foreach} </ul> <ul> {foreach $array2 as $key => $value} <li>{$key} -----> {$value}</li> {/foreach} </ul> 判断: {if $hello == 'smarty'} hello是smarty {elseif $hello == 'ci'} hello是ci {else} hello什么都不是 {/if} |
新建控制器/application/controllers/Viewwork.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Viewwork extends CI_Controller { public function index() { // 使用smarty原生的assign和display也是可以的 // $this->smarty->assign('hello', 'smarty'); // $this->smarty->display('common/test.php'); $data = array( 'hello' => 'smarty', 'array1' => array('q', 'w', 'e', 'r'), 'array2' => array('ma'=>'hehe', 'lin'=>'heihei') ); $this->smarty->view('common/test', $data); } } |
访问这个页面,成功。
—EOF—