CodeIgniter輔助之第三方類庫third_party用法分析
本文實(shí)例分析了CodeIgniter輔助之第三方類庫third_party用法。分享給大家供大家參考,具體如下:
third_party用來存放系統(tǒng)中引入的第三方類庫,類庫通常提供的功能比較豐富,相應(yīng)的學(xué)習(xí)成本也要高些,系統(tǒng)中能用到功能有限,所以建議在引入類庫時(shí)進(jìn)行適當(dāng)?shù)姆庋b,讓系統(tǒng)中更方便使用,其他人使用時(shí)只需關(guān)注擴(kuò)展的方法而無法關(guān)注具體的實(shí)現(xiàn)。以CI集成Twig模版為例吧。
首先需要下載Twig類庫,并放在third_party中,然后在libraries中進(jìn)行一次封裝,示例如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require APPPATH.'third_party/Twig/Autoloader.php'; /** * Twig模版引擎 * */ class Twig { public $twig; public $config; private $data = array(); /** * 讀取配置文件twig.php并初始化設(shè)置 * */ public function __construct($config) { $config_default = array( 'cache_dir' => false, 'debug' => false, 'auto_reload' => true, 'extension' => '.tpl', ); $this->config = array_merge($config_default, $config); Twig_Autoloader::register (); $loader = new Twig_Loader_Filesystem ($this->config['template_dir']); $this->twig = new Twig_Environment ($loader, array ( 'cache' => $this->config['cache_dir'], 'debug' => $this->config['debug'], 'auto_reload' => $this->config['auto_reload'], ) ); $CI = & get_instance (); $CI->load->helper(array('url')); $this->twig->addFunction(new Twig_SimpleFunction('site_url', 'site_url')); $this->twig->addFunction(new Twig_SimpleFunction('base_url', 'base_url')); } /** * 給變量賦值 * * @param string|array $var * @param string $value */ public function assign($var, $value = NULL) { if(is_array($var)) { foreach($val as $key => $val) { $this->data[$key] = $val; } } else { $this->data[$var] = $value; } } /** * 模版渲染 * * @param string $template 模板名 * @param array $data 變量數(shù)組 * @param string $return true返回 false直接輸出頁面 * @return string */ public function render($template, $data = array(), $return = FALSE) { $template = $this->twig->loadTemplate ( $this->getTemplateName($template) ); $data = array_merge($this->data, $data); if ($return === TRUE) { return $template->render ( $data ); } else { return $template->display ( $data ); } } /** * 獲取模版名 * * @param string $template */ public function getTemplateName($template) { $default_ext_len = strlen($this->config['extension']); if(substr($template, -$default_ext_len) != $this->config['extension']) { $template .= $this->config['extension']; } return $template; } /** * 字符串渲染 * * @param string $string 需要渲染的字符串 * @param array $data 變量數(shù)組 * @param string $return true返回 false直接輸出頁面 * @return string */ public function parse($string, $data = array(), $return = FALSE) { $string = $this->twig->loadTemplate ( $string ); $data = array_merge($this->data, $data); if ($return === TRUE) { return $string->render ( $data ); } else { return $string->display ( $data ); } } } /* End of file Twig.php */ /* Location: ./application/libraries/Twig.php */
模版的操作通常有一些配置的信息,這里通過config下的twig.php進(jìn)行配置,通過CI load library的方式加載時(shí),與類名同名的配置文件存在時(shí),會自動(dòng)以數(shù)組的方式將參數(shù)傳入類的構(gòu)造函數(shù)。
<?php // 默認(rèn)擴(kuò)展名 $config['extension'] = ".tpl"; // 默認(rèn)模版路勁 $config['template_dir'] = APPPATH . "views/"; // 緩存目錄 $config['cache_dir'] = APPPATH . "cache/twig/"; // 是否開啟調(diào)試模式 $config['debug'] = false; // 自動(dòng)刷新 $config['auto_reload'] = true; /* End of file twig.php */ /* Location: ./application/config/twig.php */
為了加載base_url site_url等函數(shù)到模版,類與CI產(chǎn)生了依賴,分離開可能更好,比如在serice中進(jìn)行一次封裝,增加一些自定義函數(shù)等,這樣其他地方、其他系統(tǒng)也就很方便復(fù)用該類了。
更多關(guān)于codeigniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》和《CI(CodeIgniter)框架進(jìn)階教程》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
- CI(CodeIgniter)模型用法實(shí)例分析
- CodeIgniter分頁類pagination使用方法示例
- CodeIgniter圖像處理類的深入解析
- codeigniter中測試通過的分頁類示例
- 使用CodeIgniter的類庫做圖片上傳
- Codeigniter整合Tank Auth權(quán)限類庫詳解
- CodeIgniter基于Email類發(fā)郵件的方法
- CodeIgniter擴(kuò)展核心類實(shí)例詳解
- php實(shí)現(xiàn)仿寫CodeIgniter的購物車類
- CI(Codeigniter)的Setting增強(qiáng)配置類實(shí)例
- Codeigniter的dom類用法實(shí)例
- CI框架(CodeIgniter)公共模型類定義與用法示例
相關(guān)文章
php下拉選項(xiàng)的批量操作的實(shí)現(xiàn)代碼
這篇文章介紹了php下拉選項(xiàng)的批量操作的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-10-10Laravel如何實(shí)現(xiàn)適合Api的異常處理響應(yīng)格式
這篇文章主要給大家介紹了關(guān)于Laravel如何實(shí)現(xiàn)適合Api的異常處理響應(yīng)格式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Laravel具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Yii中CGridView關(guān)聯(lián)表搜索排序方法實(shí)例詳解
這篇文章主要介紹了Yii中CGridView關(guān)聯(lián)表搜索排序方法,以實(shí)例形式詳細(xì)分析了CGridView關(guān)聯(lián)表搜索排序的實(shí)現(xiàn)過程與搜索結(jié)果出現(xiàn)問題的解決方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12PHP的fsockopen、pfsockopen函數(shù)被主機(jī)商禁用的解決辦法
這篇文章主要介紹了PHP的fsockopen、pfsockopen函數(shù)被主機(jī)商禁用的解決辦法,一是使用stream_socket_client函數(shù)代替,二是寫一個(gè)類似fsockopen功能的自定義函數(shù),需要的朋友可以參考下2014-07-07