Home > Backend Development > PHP Tutorial > CodeIgniter assisted third-party library third_party usage analysis, codeigniter class library_PHP tutorial

CodeIgniter assisted third-party library third_party usage analysis, codeigniter class library_PHP tutorial

WBOY
Release: 2016-07-12 09:00:01
Original
1097 people have browsed it

CodeIgniter-assisted third-party class library third_party usage analysis, codeigniter class library

This article analyzes the usage of CodeIgniter-assisted third-party class library third_party. Share it with everyone for your reference, the details are as follows:

third_party is used to store third-party class libraries introduced into the system. Class libraries usually provide richer functions, and the corresponding learning costs are higher. The functions that can be used in the system are limited, so it is recommended to do this when introducing class libraries. Proper encapsulation makes the system more convenient to use. When other people use it, they only need to pay attention to the extension method and cannot pay attention to the specific implementation. Take the CI integrated Twig template as an example.

First you need to download the Twig class library and place it in third_party, and then encapsulate it in libraries. The example is as follows:

<&#63;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 */

Copy after login

The operation of the template usually has some configuration information. Here it is configured through twig.php under config. When loading through the CI load library, when a configuration file with the same name as the class name exists, it will automatically be included in an array. The parameters are passed into the constructor of the class.

<&#63;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 */

Copy after login

In order to load base_url site_url and other functions into the template, the class has a dependency on CI. It may be better to separate it. For example, encapsulate it once in serice, add some custom functions, etc., so that other places and other systems will be very convenient. Reuse this class.

Readers who are interested in more codeigniter related content can check out the special topics on this site: "Introduction to codeigniter tutorial" and "CI (CodeIgniter) framework advanced tutorial"

I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.

Articles you may be interested in:

  • CodeIgniter configuration database.php usage example analysis
  • Detailed explanation of CodeIgniter multi-language implementation method
  • CI (CodeIgniter) Model usage example analysis
  • Detailed explanation of CodeIgniter extended core class instance
  • Notes on using CodeIgniter view
  • Detailed explanation of CodeIgniter read-write separation implementation method
  • CodeIgniter configuration SESSION Usage example analysis
  • CodeIgniter configuration routes.php usage example analysis
  • CI (CodeIgniter) simple statistics of the number of visitors implementation method
  • CodeIgniter hook usage example detailed explanation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1094767.htmlTechArticleCodeIgniter-assisted third-party class library third_party usage analysis, codeigniter class library This article analyzes the third-party class assisted by CodeIgniter Library third_party usage. Share it with everyone for your reference...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template