Codeigniter轻松整合smarty

来源:互联网 发布:农村淘宝服务站赚钱吗 编辑:程序博客网 时间:2024/06/04 18:41

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目录,结构如下:
codeigniter轻松整合smarty
(2)入口文件新增:define('ROOT', dirname(__FILE__));
(3)libraries下新建CI_Smarty.php

  1. <?php defined('BASEPATH') or die('Access restricted!');
  2.  
  3. require(APPPATH . 'libraries/smarty/Smarty.class.php');
  4.  
  5. class CI_Smarty extends Smarty {
  6.  
  7. public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '') {
  8. parent::__construct();
  9. if (is_array($template_dir)) {
  10. foreach ($template_dir as $key => $value) {
  11. $this->$key = $value;
  12. }
  13. } else {
  14. //ROOT是Codeigniter在入口文件index.php定义的本web应用的根目录
  15. $this->template_dir = $template_dir ? $template_dir : ROOT . '/templates';
  16. $this->compile_dir = $compile_dir ? $compile_dir : ROOT . '/templates_c';
  17. $this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
  18. $this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';
  19. }
  20. }
  21.  
  22. }

controller中使用:
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. // by www.phpddt.com
  3. class Welcome extends CI_Controller {
  4.  
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->library('CI_Smarty');
  9. }
  10. public function test()
  11. {
  12. $this->ci_smarty->assign('test', 'smarty');
  13. $this->ci_smarty->display('test.tpl');
  14. }
  15. }
  16.  
  17. /* End of file welcome.php */
  18. /* Location: ./application/controllers/welcome.php */

新建test.tpl模板:
  1. <html>
  2. <body>
  3. 这是 {$test} 测试
  4. </body>
  5. </html>

测试成功!
0 0
原创粉丝点击