如何将Smarty与CodeIgniter集成
来源:爱站网时间:2020-08-22编辑:网友分享
Smart的模板机制是非常强大的,一般来说,CI不需要集成其他模板标记,因为PHP本身就是一种标记,简单易用,下面爱站技术频道小编带你一起进入下文了解一下如何将Smarty与CodeIgniter集成。
Smart的模板机制是非常强大的,一般来说,CI不需要集成其他模板标记,因为PHP本身就是一种标记,简单易用,下面爱站技术频道小编带你一起进入下文了解一下如何将Smarty与CodeIgniter集成。
如何将Smarty与CodeIgniter集成
1、下载smarty-3.1.27
2 、解压smarty-3.1.27到CI项目中的application\libraries下面,其他的文件删除。
3、 在application\libraries目录下创建Ci_smarty.php文件,代码如下:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH.'libraries/smarty-3.1.27/libs/Smarty.class.php');
class Ci_smarty extends Smarty {
protected $ci;
public function __construct()
{
parent::__construct();
$this->ci = & get_instance();
$this->ci->load->config('smarty');//加载smarty的配置文件
$this->cache_lifetime =$this->ci->config->item('cache_lifetime');
$this->caching = $this->ci->config->item('caching');
$this->config_dir = $this->ci->config->item('config_dir');
$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->use_sub_dirs = $this->ci->config->item('use_sub_dirs');
$this->left_delimiter = $this->ci->config->item('left_delimiter');
$this->right_delimiter = $this->ci->config->item('right_delimiter');
}
}
4、在application\config目录下创建配置文件smarty.php,代码如下:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['cache_lifetime'] = 60;
$config['caching'] = false;
$config['template_dir'] = APPPATH .'views';
$config['compile_dir'] = APPPATH .'views/template_c';
$config['cache_dir'] = APPPATH . 'views/cache';
$config['config_dir'] = APPPATH . 'views/config';
$config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录)
$config['left_delimiter'] = '{';
$config['right_delimiter'] = '}';
5、在application\core创建MY_controller.php,代码如下:
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);
}
}
至此,配置整合工作over了,下面我们要验证是否配置成功。
7、修改application\controllers的Welcome.php,代码如下:
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends MY_controller {
public function index()
{
$test='ci 3.0.3 + smarty 3.1.27 配置成功';
$this->assign('test',$test);
$this->display('test.html');
}
}
然后,在application\views下创建test.html文件,代码如下:
{$test}
在浏览器地址栏中输入:http://localhost/index.php/Welcome
结果显示:
ci 3.0.3 + smarty 3.1.27 配置成功
如何将Smarty与CodeIgniter集成,看完以上内容您都了解了吗?想了解更多请关注js.aizhan.com,我们会一一为你解答。
上一篇:实现PHP的采集功能的示例代码
下一篇:PHP钩子与简单分发方式实例分析
