Third-party class library third_party usage analysis assisted by CodeIgniter

Source: Internet
Author: User
: This article mainly introduces the usage analysis of third-party class library third_party assisted by CodeIgniter. For more information about PHP tutorials, see. This article analyzes the usage of third-party class library third_party assisted by CodeIgniter. We will share this with you for your reference. The details are as follows:

Third_party is used to store third-party class libraries introduced in the system. the class libraries generally provide a wide range of functions, and the corresponding learning costs are also higher. the functions used in the system are limited, therefore, we recommend that you encapsulate the class library to make it easier to use in the system. when using the class library, you only need to pay attention to the extended method instead of the specific implementation. Take CI integration with the Twig template as an example.

Download the Twig class library, put it in third_party, and encapsulate it in libraries. for 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 the configuration file twig. php and initialize the setting **/public function _ construct ($ config) {$ config_default = array ('cache _ dir' => false, 'debug' => false, 'auto _ reload' => true, 'extension' => '. tpl ',); $ this-> config = ar Ray_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 (n Ew Twig_SimpleFunction ('site _ url', 'site _ url'); $ this-> twig-> addFunction (new Twig_SimpleFunction ('base _ url ', 'base _ url');}/*** assign a variable value ** @ 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 ;}} /*** template rendering ** @ param string $ template Template name * @ param array $ data variable array * @ param string $ return true return false direct output page * @ 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) ;}}/*** get the modulo Version name ** @ 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 ;} /*** string rendering ** @ param string $ string the string to be rendered * @ param array $ data variable array * @ param string $ return true returns false to directly output the page * @ 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 */

The template operation usually has some configuration information. here we use the twig under config. php configuration. when the CI load library method is used for loading, if a configuration file with the same name as the class name exists, the parameter is automatically input to the class constructor in array mode.

<? Php // Default extension $ config ['extension'] = ". tpl "; // Default template path $ config ['Template _ dir'] = APPPATH. "views/"; // cache directory $ config ['cache _ dir'] = APPPATH. "cache/twig/"; // whether to enable debug mode $ config ['debug'] = false; // automatically refresh $ config ['auto _ reload'] = true; /* End of file twig. php * // * Location :. /application/config/twig. php */

In order to load functions such as base_url site_url to the template, the class and CI are dependent, and the separation may be better. for example, the class is encapsulated in serice and some custom functions are added, in this way, the class can be easily reused in other places and systems.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.