This article mainly introduces the method of integrating smarty of CodeIgniter, analyzes the steps of CodeIgniter3.0.3 integration Smarty3.1.27 with the example form, and the related setting skills, the friends who need can refer to the following
This paper describes the method of CodeIgniter integration Smarty. Share to everyone for your reference, as follows:
CI3.0.2 after the release feeling template class is still not very useful, and can not compile. Smarty powerful, with the habit of smarty tags, generally difficult to give up, and, it is possible to compile file execution, fast, we can integrate them to make up for the lack of CI template function. We have integrated the CI version 3.0.3 and smarty version 3.1.27. The integration process is described below.
1. Download smarty-3.1.27
2, decompression smarty-3.1.27 to the CI project in the application\libraries below, the other files deleted.
3, create the ci_smarty.php file in the Application\libraries directory, the code is as follows:
if (! defined (' BasePath ')) exit (' No Direct script access allowed '); require (APPPATH. ' libraries/smarty-3.1.27/libs/ Smarty.class.php '), class Ci_smarty extends Smarty {protected $ci; public Function __construct () {parent::__construct (); $this->ci = & Get_instance (); $this->ci->load->config (' smarty ');//load Smarty configuration file $this->cache_lifetime = $this->ci->config- >item (' Cache_lifetime '); $this->caching = $this->ci->config->item (' caching '); $this->config_dir = $this->ci->config->item (' Config_dir '); $this->template_dir = $this->ci->config->item (' Template_dir '); $this->compile_dir = $this->ci->config->item (' Compile_dir '); $this->cache_dir = $this->ci->config->item (' Cache_dir '); $this->use_sub_dirs = $this->ci->config->item (' use_sub_dirs '); $this->left_delimiter = $this->ci->config->item (' Left_delimiter '); $this->right_delimiter = $this->ci->config->item (' Right_delimiter '); }}
4, in the Application\config directory to create the configuration file smarty.php, the code is as follows:
if (! defined (' BasePath ')) exit (' No Direct script access allowed '); $config [' cache_lifetime '] =; $config [' caching '] = FA LSE; $config [' template_dir '] = APPPATH. ' Views '; $config [' compile_dir '] = APPPATH. ' Views/template_c '; $config [' cache_dir '] = APPPATH. ' Views/cache '; $config [' config_dir '] = APPPATH. ' Views/config '; $config [' use_sub_dirs '] = false; subdirectory variables (whether subdirectories are generated in the cache folder) $config [' left_delimiter '] = ' {'; $config [' right_delimiter '] = '} ';
5, create the my_controller.php in Application\core, the code is as follows:
Class My_controller extends Ci_controller {public Function __construct () {parent::__construct ()}, Public function Assig N ($key, $val) {$this->ci_smarty->assign ($key, $val), Public function display ($html) {$this->ci_smarty-> Display ($html); }}
At this point, the configuration integration work over, we want to verify the configuration is successful.
7, modify the Application\controllers welcome.php, the code is as follows:
Defined (' BasePath ') or exit (' No Direct script access allowed '); class Welcome extends My_controller {public Function index {$test = ' ci 3.0.3 + smarty 3.1.27 configuration succeeded '; $this->assign (' Test ', $test); $this->display (' test.html ');}}
Then, create the test.html file under Application\views, with the following code:
{$test}
In the browser address bar, type:http://localhost/index.php/Welcome
The results show:
CI 3.0.3 + smarty 3.1.27 configuration succeeded
Done!
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!