PHP CodeIngiter 2.x.x 框架与 Smarty 3.x.x 模板引擎整合

目前CI框架已经推出了2.0的版本了。一直想尝试修改我的网站的核心。经过测试可以正常使用,以下提供配置方案和已经配置好的文件,
下面是配置步骤的详细说明一下:
第一步:安装CodeIngiter。 这个不需要详细说,下载地址为:http://codeigniter.com/downloads/
第二步:下载最新版本的 Smarty库,下载地址:http://www.smarty.net/download
第三步:在安装好的ci项目的根目录下的入口文件 index.php文件开头添加如下代码

define('APP_NAME','application');

if(!defined('ROOT'))define('ROOT',dirname(__FILE__));

if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);

第四步:在安装好的ci项目的根目录下application\config下添加smarty.php配置文件

<php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/* smarty 设置 */
	$config['caching']			=   false;
	$config['cache_lifetime']		=   3600;
	$config['template_dir']                 =   ROOT.DS.APP_NAME.DS.'views'.DS;
	$config['compile_dir']  		=   ROOT.DS.APP_NAME.DS.'cache'.DS.'views_c'.DS;
	$config['cache_dir ']    		=   ROOT.DS.APP_NAME.DS.'cache'.DS.'smarty_cache'.DS;
	$config['left_delimiter'] 		=   '<!--{';
	$config['right_delimiter ']		=   '}---->';

第五步:在安装好的ci项目的根目录下\application\libraries,把下载好的smarty文件包中 libs 放到这里,并改名为smarty
第六步:在安装好的ci项目的根目录下\application\libraries下添加smarty扩展类CI_Smarty.php

<?php
require (ROOT.DS.APPPATH."libraries".DS."smarty".DS."Smarty.class.php");
class CI_Smarty extends Smarty
{
	protected $ci;

	public function __construct()
	{
	    parent::__construct();
	    $this->ci = & get_instance();
	    $this->ci->load->config('smarty');
	    self::loadSetting();
	}

 	public function loadSetting()
 	{
 		$this->caching		=   $this->ci->config->item('caching');
 		$this->cache_lifetime	=   $this->ci->config->item('cache_lifetime');
  		$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->left_delimiter 	=   $this->ci->config->item('left_delimiter');
  		$this->right_delimiter 	=   $this->ci->config->item('right_delimiter');
	}

}

第七步:在application\config\autoload.php,设置自动加载smarty扩展类CI_Smarty.php

$autoload['libraries'] = array('ci_smarty');

第八步:在application\core 添加 MY_Controller.php 文件

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class MY_Controller extends CI_Controller
	{

		public function __construct()
		{
			parent::__construct();
		}

		public function assign($key,$val)
		{
			$this->ci_smarty->assign($key,$val);
		}

		public function display($html)
		{
			$this->ci_smarty->display($html);
		}

	}

第九步:已经配置完成 开始测试 在 application\controllers\welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Welcome extends MY_Controller {

		public function index()
		{
			$this->assign("hello","<a href='http://www.pvsky.com/blog'>我的博客</a>");
			$this->display("test.html");
		}
	}

模板文件 \application\views\test.html

    <html>
    <head></head>
    <body>
	<!--{$hello}-->
    </body>
    </html>

访问你的本地 index.php 就可以看到

转载于:https://my.oschina.net/mickelfeng/blog/605847

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值