CodeIgniter集成smarty的方法詳解
本文實(shí)例講述了CodeIgniter集成smarty的方法。分享給大家供大家參考,具體步驟如下:
1.下載smarty
解壓到ci的libraries目錄 如:
ci/application/libraries/Smarty-2.6.20
2.編寫(xiě)Mysmarty.php 自己的類(lèi)庫(kù)文件
代碼如下:
<?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自動(dòng)加載smarty
$autoload['libraries'] = array('database', 'mysmarty');
或者 使用模板時(shí)再自己加載smarty
$this->load->library("mysmarty");
4.smarty變量賦值 display模板
$this->mysmarty->assign('test', 'Hello World.');
$this->mysmarty->view('smarty');
注:images css 等外部資源文件 放在ci系統(tǒng)文件夾外 網(wǎng)站根目錄下
最好用:
$this->load->helper('url');
base_url()來(lái)訪問(wèn):
base_url()."images/xxx.jpg"
不要放到system里
補(bǔ)充:小編在這里推薦一款本站的php格式化美化的排版工具幫助大家在以后的PHP程序設(shè)計(jì)中進(jìn)行代碼排版:
php代碼在線格式化美化工具:
http://tools.jb51.net/code/phpformat
另外,由于php屬于C語(yǔ)言風(fēng)格,因此下面這款工具同樣可以實(shí)現(xiàn)php代碼的格式化:
C語(yǔ)言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《ThinkPHP入門(mén)教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP SESSION跨頁(yè)面?zhèn)鬟f失敗解決方案
這篇文章主要介紹了PHP SESSION跨頁(yè)面?zhèn)鬟f失敗解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
PHP獲取用戶(hù)客戶(hù)端真實(shí)IP的解決方案
獲取客戶(hù)端ip其實(shí)不是個(gè)簡(jiǎn)單的活兒,因?yàn)榇嬖贗p欺騙,和代理問(wèn)題,所以獲取客戶(hù)端的IP的真實(shí)性會(huì)打折扣的,不能百分百準(zhǔn)確.但是我們盡量使用比較完善的方法獲取客戶(hù)的ip,下面小編給大家分享PHP獲取用戶(hù)客戶(hù)端真實(shí)IP的方法,一起看看吧2016-10-10
php metaphone()函數(shù)及php localeconv() 函數(shù)實(shí)例解析
這篇文章主要介紹了php metaphone()函數(shù)及php localeconv() 函數(shù)實(shí)例解析的相關(guān)資料,需要的朋友可以參考下2016-05-05
詳解Yii2.0使用AR聯(lián)表查詢(xún)實(shí)例
使用swoole擴(kuò)展php websocket示例
Zend Framework使用Zend_Loader組件動(dòng)態(tài)加載文件和類(lèi)用法詳解
淺談Laravel POST,PUT,PATCH 路由的區(qū)別

