In this paper, the Third_party usage of CodeIgniter Auxiliary Third party class library is analyzed. Share to everyone for your reference, specific as follows:
Third_party used to store the system introduced by the Third-party class library, the class library usually provides rich functions, the corresponding learning costs are higher, the system can be used to a limited number of functions, so it is recommended to introduce a class library for appropriate encapsulation, so that the system more convenient to use, Others need to focus on the extended approach rather than 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 the configuration file twig.php and initialize the settings */Public function __construct ($config) {$config _default = array (' C
Ache_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 '), ' Debu
G ' => $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 value to variable * * @param string|array $var * @param string $value/Public Function assign ($var, $val
UE = NULL) {if (Is_array ($var)) {foreach ($val as $key => $val) {$this->data[$key] = $val;
} else {$this->data[$var] = $value; }/** * Stencil rendering * * @param string $template template name * @param array $data variable arrays * @param string $return true return Return FALSE Direct Output page * @return string/Public function render ($template, $data = Array (), $return = FALSE) {$tem
Plate = $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 Stencil 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 Render * * @param string $string strings to render * @param array $data variable arrays * @param string $return true return Return FALSE Direct Output 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/*
Template operations usually have some configuration information, here through the config under the twig.php configuration, through the CI load library, the way the configuration file with the same name exists, automatically as an array of parameters passed into the class constructor.
<?php
//default extension
$config [' extension '] = ". TPL";
Default Template Road Strength
$config [' template_dir '] = AppPath. "views/";
Cache directory
$config [' cache_dir '] = AppPath. "cache/twig/";
Whether debug mode is turned on
$config [' debug '] = false;
Auto Refresh
$config [' auto_reload '] = true;
/* End of File twig.php
////* Location:./application/config/twig.php/*
In order to load Base_url Site_url and other functions to the template, the class and CI generated dependencies, the separation may be better, such as in the Serice of a package, add some custom functions, so that other places, other systems are very convenient to reuse the class.
More readers interested in CodeIgniter-related content can view the site topics: "CodeIgniter Introductory Course" and "CI (CodeIgniter) Framework Advanced Course"
I hope this article will help you with the PHP program design based on CodeIgniter framework.