CodeIgniter Auxiliary third-party class library Third_party usage analysis, CodeIgniter class Library _php Tutorial

Source: Internet
Author: User
Tags codeigniter

CodeIgniter Auxiliary third-party class library Third_party usage analysis, CodeIgniter class library


This paper analyzes the use of CodeIgniter auxiliary third-party class library Third_party. Share to everyone for your reference, as follows:

Third_party used to store the third-party class library introduced in the system, the class library usually provides a richer function, the corresponding learning cost is higher, the system can use limited functionality, so it is recommended to introduce the class library when appropriate encapsulation, so that the system more convenient to use, Other people only need to focus on the extended approach and not the specific implementation. Take CI integrated twig template as an example.

First, you need to download the Twig class library, put it in Third_party, and then encapsulate it in libraries, as shown in the following example:

<?php if (! defined (' BasePath ')) exit (' No Direct script access allowed '); require APPPATH. ' third_party/twig/  autoloader.php ';/** * Twig template engine * */class twig{public $twig;  public $config;  Private $data = Array (); /** * Read configuration file twig.php and initialize settings * */Public Function __construct ($config) {$config _default = array (' Cache_di    R ' = False, ' debug ' = False, ' auto_reload ' = ' = ', ' 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 ')); /** * Assign a value to a variable * * @param string|array $var * @param string $value */Public Function assign ($var, $value = NU      LL) {if (Is_array ($var)) {foreach ($val as $key = = $val) {$this->data[$key] = $val;    }} else {$this->data[$var] = $value; }}/** * Template render * * @param string $template name * @param array $data variable array * @param string $return true returns false Direct Output Page * @return String */Public function render ($template, $data = Array (), $return = FALSE) {$template = $thi    S->twig->loadtemplate ($this->gettemplatename ($template));    $data = Array_merge ($this->data, $data);    if ($return = = = TRUE) {return $template->render ($data);    } else {return $template->display ($data); }}/** * Gets the template name * * @param string $template */Public Function Gettemplatename ($template) {$default _ext_l En = StrlEn ($this->config[' extension '); if (substr ($template,-$default _ext_len)! = $this->config[' extension ') {$template. = $this->config[' extension '    ];  } return $template; }/** * String Rendering * * @param string $string required to be rendered * @param array $data variable array * @param string $return true returns Fals E Direct Output Page * @return String */Public Function parse ($string, $data = Array (), $return = FALSE) {$string = $this-&G    T;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 */

The template operation usually has some configuration information, which is configured by the twig.php under Config, when loaded by the CI load library, when a configuration file with the same name as the class name exists, the parameter is automatically passed to the constructor of the class as an array.

<?php//default extension $config[' extension '] = ". TPL";//default stencil Road Strength $config[' template_dir '] = APPPATH. "views/";//cache directory $config[' cache_dir ') = APPPATH. "cache/twig/";//whether to turn on debug mode $config[' Debug ' = false;//auto Refresh $config[' auto_reload '] = true;/* End of file twig.php *//* Locati On:./application/config/twig.php */

In order to load functions such as Base_url site_url to the template, classes and CI have generated a dependency, it may be better to leave, for example, in the Serice in a package, add some custom functions, etc., so that other places, other systems can easily reuse the class.

More interested in CodeIgniter related content readers can view this site topic: "CodeIgniter Introductory Tutorial" and "CI (codeigniter) Framework Advanced Tutorial"

It is hoped that this article is helpful to the PHP program design based on CodeIgniter framework.

Articles you may be interested in:

    • Example analysis of database.php usage of codeigniter configuration
    • CodeIgniter Multi-language Implementation method detailed
    • CI (CodeIgniter) Model Usage Example analysis
    • CodeIgniter Extending Core class examples
    • CodeIgniter View Usage Considerations
    • CodeIgniter Read and write separation implementation method detailed
    • Example analysis of session usage of CodeIgniter configuration
    • Example analysis of routes.php usage of codeigniter configuration
    • CI (CodeIgniter) Simple statistics access to the number of implementation methods
    • CodeIgniter Hook Usage Example detailed

http://www.bkjia.com/PHPjc/1094767.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094767.html techarticle CodeIgniter Auxiliary third-party library third_party usage analysis, CodeIgniter class Library This paper analyzes the CodeIgniter auxiliary third-party class library Third_party usage. Share for everyone to join us ...

  • Related Article

    Cloud Intelligence Leading the Digital Future

    Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

    Register Now >

    Starter Package

    SSD Cloud server and data transfer for only $2.50 a month

    Get Started >

    Alibaba Cloud Free Trial

    Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

    Learn more >

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.