本文实例分析了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程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!