本文實例分析了CodeIgniter輔助之第三方類庫third_party用法。分享給大家供大家參考,具體如下:
third_party用來存放系統中引入的第三方類庫,類庫通常提供的功能比較豐富,相應的學習成本也要高些,系統中能用到功能有限,所以建議在引入類庫時進行適當的封裝,讓系統中更方便使用,其他人使用時只需關注擴展的方法而無法關注具體的實現。以CI集成Twig模版為例吧。
首先需要下載Twig類庫,並放在third_party中,然後在libraries中進行一次封裝,示例如下:
<?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並初始化設置
*
*/
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 變量數組
* @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 變量數組
* @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進行配置,通過CI load library的方式加載時,與類名同名的配置文件存在時,會自動以數組的方式將參數傳入類的構造函數。
<?php // 默認擴展名 $config['extension'] = ".tpl"; // 默認模版路勁 $config['template_dir'] = APPPATH . "views/"; // 緩存目錄 $config['cache_dir'] = APPPATH . "cache/twig/"; // 是否開啟調試模式 $config['debug'] = false; // 自動刷新 $config['auto_reload'] = true; /* End of file twig.php */ /* Location: ./application/config/twig.php */
為了加載base_url site_url等函數到模版,類與CI產生了依賴,分離開可能更好,比如在serice中進行一次封裝,增加一些自定義函數等,這樣其他地方、其他系統也就很方便復用該類了。
更多關於codeigniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》和《CI(CodeIgniter)框架進階教程》
希望本文所述對大家基於CodeIgniter框架的PHP程序設計有所幫助。