CI轻松整合smarty
2015-10-05 20:34
471 查看
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目录,结构如下:
(2)入口文件新增:define('ROOT', dirname(__FILE__));
(3)libraries下新建CI_Smarty.php
<?php defined('BASEPATH') or die('Access restricted!');
require(APPPATH . 'libraries/smarty/Smarty.class.php');
class CI_Smarty extends Smarty {
public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '') {
parent::__construct();
if (is_array($template_dir)) {
foreach ($template_dir as $key => $value) {
$this->$key = $value;
}
} else {
//ROOT是Codeigniter在入口文件index.php定义的本web应用的根目录
$this->template_dir = $template_dir ? $template_dir : ROOT . '/templates';
$this->compile_dir = $compile_dir ? $compile_dir : ROOT . '/templates_c';
$this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
$this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';
}
}
}
controller中使用:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// by www.phpddt.com
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('CI_Smarty');
}
public function test()
{
$this->ci_smarty->assign('test', 'smarty');
$this->ci_smarty->display('test.tpl');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
新建test.tpl模板:
<html>
<body>
这是 {$test} 测试
</body>
</html>
测试成功
原文出自:http://www.phpddt.com/php/ci-smarty.html
第一步:下载codeigniter最新版本:http://codeigniter.org.cn/downloads
第二步:下载smarty最新版本:http://www.smarty.net/download
第三步:
配置步骤:
(1)将smarty拷贝到application/libraries下,然后再根目录下下新建templates,templates_c,config,cache目录,结构如下:
(2)入口文件新增:define('ROOT', dirname(__FILE__));
(3)libraries下新建CI_Smarty.php
<?php defined('BASEPATH') or die('Access restricted!');
require(APPPATH . 'libraries/smarty/Smarty.class.php');
class CI_Smarty extends Smarty {
public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '') {
parent::__construct();
if (is_array($template_dir)) {
foreach ($template_dir as $key => $value) {
$this->$key = $value;
}
} else {
//ROOT是Codeigniter在入口文件index.php定义的本web应用的根目录
$this->template_dir = $template_dir ? $template_dir : ROOT . '/templates';
$this->compile_dir = $compile_dir ? $compile_dir : ROOT . '/templates_c';
$this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
$this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';
}
}
}
controller中使用:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// by www.phpddt.com
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('CI_Smarty');
}
public function test()
{
$this->ci_smarty->assign('test', 'smarty');
$this->ci_smarty->display('test.tpl');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
新建test.tpl模板:
<html>
<body>
这是 {$test} 测试
</body>
</html>
测试成功
原文出自:http://www.phpddt.com/php/ci-smarty.html
相关文章推荐
- 像Bill Gates一样思考集装箱
- 矩阵快速幂 CodeForces - 582B Once Again...
- UVA 11995 I Can Guess the Data Structure!
- 多线程编程
- FZU 1686 神龙的难题 (DLX求重复覆盖)
- 在编写JSON数据时java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeExcepti
- UVA 116 Unidirectional TSP
- OpenCV支持的AVI文件格式
- 华为面试题算什么,这个背会了外企随便进
- LDA模型简介、源码分析及实验
- Git简介
- HTC迅速衰落:台湾科技行业怎么了
- linux vim/vi编辑器指令
- 设计思维的要素:优化愿景(Vision)
- 设计思维的要素:优化愿景(Vision)
- 设计思维的要素:新鲜事物(Unknowns)
- 设计思维的要素:新鲜事物(Unknowns)
- poj 2182 Lost Cows(段树精英赛的冠军)
- 总裁决策大数据:新思维&技术
- 总裁决策大数据:新思维&技术