codeigniter配置smarty模板

本文详细介绍如何在CodeIgniter框架中集成Smarty模板引擎,包括安装步骤、配置方法及简单示例,帮助开发者快速上手。

codeigniter是一个比较不错的轻量级的php开发web frame,我将ci结合smarty模板解释器使用,现在ci配置smarty的方法给出:

①下载smarty的源码包并且解压

②将smarty源码包中的libs核心类库重命名为Smarty并且经文件夹copy到ci的项目目录下的libraries下,同时在libraries文件夹下建立Cismarty.php文件。

③在Cismarty.php文件中写入如下的code

<?php
 if(!defined('BASEPATH'))exit('no dir');
 require_once (APPPATH.'libraries/Smarty/Smarty.class.php');
 class Cismarty extends Smarty
 {
  protected $ci;
  function __construct()
  {
   parent::__construct();
   $this->ci = &get_instance();
   $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('caching_dir');
   $this->config_dir = $this->ci->config->item('config_dir');
   $this->template_ext = $this->ci->config->item('template_ext');
   $this->caching = $this->ci->config->item('caching');
   $this->cache_lifetime = $this->ci->config->item('cache_lifetime');
  }
 }

④在项目目录下的cofig文件夹建立smarty.php文件,并且写入如下代码:

<?php
 if (! defined('BASEPATH')) exit('no direct base');
 $config['template_dir'] = APPPATH.'views';
 $config['compile_dir'] = FCPATH.'template_c';
 $config['caching_dir'] = FCPATH.'cache';
 $config['config_dir'] = FCPATH.'config';
 $config['caching'] = false;
 $config['cache_lifetime'] = '60';
 $config['template_ext'] = '.html';

⑤在项目目录下的config文件夹中的autoload.php文件中写入以下代码:

$autoload['config'] = array('smarty');
 $autoload['libraries'] = array('cismarty');

至此配置完毕,以下给出简单的测试程序

	class Start extends CI_Controller
	{
		function __construct()
		{
			parent::__construct();
			$this->load->database();
//			$this->load->library('cismarty');

		}

		function smarty()
		{
			$title = 'title';
			$this->cismarty->assign('title',$title);
			$this->cismarty->display("test.html");
		}
	} 

现在自己在application下的views下建立一个test.html文件,传一个数据过去,如果配置正确那么可以看到测试页面和数据。

本文作者:爱帮竞人才网 3A主机创始人 高学平

转载于:https://www.cnblogs.com/ibjrc/archive/2011/07/15/2107891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值