时时商务社区
标题:
CodeIgniter辅助之第三方类库third_party用法分析
[打印本页]
作者:
阿情
时间:
2018-2-14 05:39
本文实例分析了CodeIgniter辅助之第三方类库third_party用法。分享给大家供大家参考,具体如下:
third_party用来存放系统中引入的第三方类库,类库通常提供的功能比较丰富,相应的学习成本也要高些,系统中能用到功能有限,所以建议在引入类库时进行适当的封装,让系统中更方便使用,其他人使用时只需关注扩展的方法而无法关注具体的实现。以CI集成Twig模版为例吧。
首先需要下载Twig类库,并放在third_party中,然后在libraries中进行一次封装,示例如下:
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的方式加载时,与类名同名的配置文件存在时,会自动以数组的方式将参数传入类的构造函数。
为了加载base_url site_url等函数到模版,类与CI产生了依赖,分离开可能更好,比如在serice中进行一次封装,增加一些自定义函数等,这样其他地方、其他系统也就很方便复用该类了。
更多关于codeigniter相关内容感兴趣的读者可查看本站专题:《
codeigniter入门教程
》和《
CI(CodeIgniter)框架进阶教程
》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
CodeIgniter图像处理类的深入解析
codeigniter中测试通过的分页类示例
使用CodeIgniter的类库做图片上传
Codeigniter整合Tank Auth权限类库详解
Codeigniter的dom类用法实例
php实现仿写CodeIgniter的购物车类
CI(Codeigniter)的Setting增强配置类实例
CodeIgniter扩展核心类实例详解
CI(CodeIgniter)模型用法实例分析
CodeIgniter分页类pagination使用方法示例
CodeIgniter基于Email类发邮件的方法
CI框架(CodeIgniter)公共模型类定义与用法示例
欢迎光临 时时商务社区 (http://bbs.4435.cn/)
Powered by Discuz! X3.2