smarty的模板机制很强大,一般情况下CI无需整合其他模板标签,因为PHP本身就是一种标签,简单易用。codeigniter整合smarty教程(我用的都是最新版本)如下:
第一步:下载codeigniter最新版本:http://codeigniter.org.cn/downloads
第二步:下载smarty最新版本:http://www.smarty.net/download
第三步:
配置步骤:
(1)将smarty拷贝到application/libraries下,然后再根目录下下新建templates,templates_c,config,cache目录,结构如下:
(2)入口文件新增:define('ROOT', dirname(__FILE__));
(3)libraries下新建CI_Smarty.php
- <?php defined('BASEPATH') or die('Access restricted!');
-
- require(APPPATH . 'libraries/smarty/Smarty.class.php');
-
- class CI_Smarty extends Smarty {
-
- public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '') {
- parent::__construct();
- if (is_array($template_dir)) {
- foreach ($template_dir as $key => $value) {
- $this->$key = $value;
- }
- } else {
- //ROOT是Codeigniter在入口文件index.php定义的本web应用的根目录
- $this->template_dir = $template_dir ? $template_dir : ROOT . '/templates';
- $this->compile_dir = $compile_dir ? $compile_dir : ROOT . '/templates_c';
- $this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
- $this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';
- }
- }
-
- }
controller中使用:
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- // by www.phpddt.com
- class Welcome extends CI_Controller {
-
- public function __construct()
- {
- parent::__construct();
- $this->load->library('CI_Smarty');
- }
-
-
- public function test()
- {
-
- $this->ci_smarty->assign('test', 'smarty');
- $this->ci_smarty->display('test.tpl');
-
- }
- }
-
- /* End of file welcome.php */
- /* Location: ./application/controllers/welcome.php */
新建test.tpl模板:
测试成功!