ci2 整合smarty3.0

(1)下载smarty ,这儿下载的是 smarty-3.1.29

(2)解压到 application/librarys/文件夹下面,形成 application/librarys/smarty-3.1.29/

(3)在application/librarys/Cismarty.php新建 Cismarty.php文件

Cismarty.php 文件修改

<?php

if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');
require_once( APPPATH . 'libraries/smarty-3.1.29/libs/Smarty.class.php' );

class Cismarty extends Smarty
{
	protected $ci;
	public function __construct()
	{
		parent::__construct();
		$this->ci =& get_instance();
		$this->ci->load->config('smarty');// 加载smarty 的配置文件
		// 获取相关的配置项
		$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->config_dir     = $this->ci->config->item('config_dir');
		$this->caching        = $this->ci->config->item('caching');
		$this->cache_lifetime = $this->ci->config->item('lefetime');
		$this->right_delimiter= $this->ci->config->item('right_delimiter');
		$this->left_delimiter = $this->ci->config->item('left_delimiter');
	}
}

(4)配置smarty配置项

<?php
if( !defined('BASEPATH')) exit('No direct script access allowed');
	
$config['theme']    = 'default';
$config['template_dir']    = FCPATH . 'www/temp';
$config['compile_dir']    = FCPATH . 'www/temp_c';
$config['cache_dir']    = FCPATH . 'www/cache';
$config['config_dir']    = FCPATH . 'www/configs';
$config['caching']    = false;
$config['lefttime']    = 0;
$config['left_delimiter'] = '<{';
$config['right_delimiter'] = '}>';

(5) 在index.php同级目录下,建立文件夹

www

www/temp

www/temp_c

www/cache/

www/configs

(6)开启自由加载,application/config/autoload.php

中修改代码  $autoload['libraries'] = array('Cismarty');

(7)验证

新建

application/core/MY_Controller.php

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

class MY_Controller extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

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

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

修改application/controller.php

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

class Welcome extends MY_Controller 
{


	public function index()
	{
		$say = 'hello world';
		$this->assign('say', $say);
		$this->display("index.html");
	}
}

新建文件www/temp/index.html

<!DOCTYPE>
<html>
<head>
<title></title>
</head>
<body>
<{$say}>
</body>
</html>

最后 >sudo chmod -R 777 www

访问localhost/index.php

输出 hello world

配置完成。

你可能感兴趣的:(ci2 整合smarty3.0)