《PHP實例:CodeIgniter集成smarty的方法詳解》要點:
本文介紹了PHP實例:CodeIgniter集成smarty的方法詳解,希望對您有用。如果有疑問,可以聯系我們。
PHP實例本文實例講述了CodeIgniter集成smarty的方法.分享給大家供大家參考,具體步驟如下:
PHP實例1.下載smarty
PHP實例解壓到ci的libraries目錄 如:
PHP實例ci/application/libraries/Smarty-2.6.20
PHP實例2.編寫Mysmarty.php 自己的類庫文件
PHP實例代碼如下:
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
?>
PHP實例3.在autoload.php讓ci自動加載smarty
PHP實例
$autoload['libraries'] = array('database', 'mysmarty');
PHP實例或者 使用模板時再自己加載smarty
PHP實例
$this->load->library("mysmarty");
PHP實例4.smarty變量賦值 display模板
PHP實例
$this->mysmarty->assign('test', 'Hello World.');
$this->mysmarty->view('smarty');
PHP實例注:images css 等外部資源文件 放在ci系統文件夾外 網站根目錄下
PHP實例最好用:
PHP實例
$this->load->helper('url');
PHP實例base_url()來訪問:
PHP實例
base_url()."images/xxx.jpg"
PHP實例不要放到system里
PHP實例補充:小編在這里推薦一款本站的php格式化美化的排版工具幫助大家在以后的PHP程序設計中進行代碼排版:
PHP實例php代碼在線格式化美化工具:
PHP實例http://tools.jb51.net/code/phpformat
PHP實例另外,由于php屬于C語言風格,因此下面這款工具同樣可以實現php代碼的格式化:
PHP實例C語言風格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
PHP實例更多關于CodeIgniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優秀開發框架總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork框架入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP實例希望本文所述對大家基于CodeIgniter框架的PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/6319.html