这篇文章将为大家详细讲解有关CodeIgniter集成smarty的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
具体步骤如下:
1.下载smarty
解压到ci的libraries目录 如:
ci/application/libraries/Smarty-2.6.20
2.编写Mysmarty.php 自己的类库文件
代码如下:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require "Smarty-2.6.20/libs/Smarty.class.php";
/**
* @file system/application/libraries/Mysmarty.php
*/
class Mysmarty extends Smarty
{
function Mysmarty()
{
$this->Smarty();
$config =& get_config();
// absolute path prevents "template not found" errors
$this->template_dir = (!empty($config['smarty_template_dir']) ? $config['smarty_template_dir'] : BASEPATH . 'application/views/');
$this->compile_dir = (!empty($config['smarty_compile_dir']) ? $config['smarty_compile_dir'] : BASEPATH . 'cache/');
//use CI's cache folder
if (function_exists('site_url')) {
// URL helper required
$this->assign("site_url", site_url()); // so we can get the full path to CI easily
}
}
/**
* @param $resource_name string
* @param $params array holds params that will be passed to the template
* @desc loads the template
*/
function view($resource_name, $params = array()) {
if (strpos($resource_name, '.') === false) {
$resource_name .= '.html';
}
if (is_array($params) && count($params)) {
foreach ($params as $key => $value) {
$this->assign($key, $value);
}
}
// check if the template file exists.
if (!is_file($this->template_dir . $resource_name)) {
show_error("template: [$resource_name] cannot be found.");
}
return parent::display($resource_name);
}
} // END class smarty_library
?>
3.在autoload.php让ci自动加载smarty
$autoload['libraries'] = array('database', 'mysmarty');
或者 使用模板时再自己加载smarty
$this->load->library("mysmarty");
4.smarty变量赋值 display模板
$this->mysmarty->assign('test', 'Hello World.');
$this->mysmarty->view('smarty');
注:images css 等外部资源文件 放在ci系统文件夹外 网站根目录下
最好用:
$this->load->helper('url');
base_url()来访问:
base_url()."images/xxx.jpg"
不要放到system里。
关于“CodeIgniter集成smarty的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。