* * The best way to integrate Smarty with the CI framework of the "CI Framework" PHP

Source: Internet
Author: User
Tags autoload

Because the CI comes with the template function is not very convenient, so we generally adopt integrated smarty way to compensate for the shortcomings of CI. I read a lot of CI integrated smarty on the Internet, including our CI forum in the essence of a post http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid= 10345. I compare these tutorials, I think the following plan is the best of all, strongly recommend to everyone (of course, I have to take the solution) Source: http://www.cnmiss.cn/?p=261 in the original, some mistakes, I have made a correction in this article------------------------------------------------------------------------------------------------------------- ----------------------------------------below the reason why I think it is more excellent, compare this program with our forum, you will find that the solution is a little more that it expands the core class, It extends the Smarty class method assign and display to the controller of CI, so we can use the smarty in CI like this: Public Function index () {//$this-&GT;LOAD-&G        T;view (' welcome_message ');        $data [' title '] = ' title ';        $data [' num '] = ' 123456789 '; $this->cismarty->assign (' data ', $data);        You can also $this->assign (' data ', $data);        $this->assign (' tmp ', ' hello '); $this->cismarty->display (' test.html ');    You can also $this->display (' test.html '); The simple extension of the core controller class, the use of smary in CI and the use of Smarty are the same, this is a great advantage ah. And the extension from the Core class library, you can see that the author's understanding of the CI framework is very good. According to this article, I not only successfully integrated the Smaty, but also further enhanced the understanding of CI. Moreover, the scheme puts the Smarty configuration file into the Config directory of the CI framework, which is very canonical for both. The result is a "seamless integration of CI and smaty". --------------------------------------------------------------------------------------------------------------- ---------------------------------------The following is a specific tutorial://I made some changes on the basis of the original text, correcting some errors in the original text note that there are '//' places below, which I have modified myself, Or the place where you add yourself. CI version: 2.1.4//At this time the latest version Smarty version: Smarty-2.6.26//Because I used this version, in order to take care of their own habits, there is no use of the latest Smaty version, we understand the extension principle, you can choose to use the Smatry version. 1, to the corresponding site to download Smarty source package;//I use the Smarty-2.6.262 here, the source package inside the Libs folder copy to the CI project directory under the Libraries folder, and renamed to smarty-2.6.26;//3, Create a new file cismarty.php within the Libraries folder of the project directory, with the following contents: <?php if (!defined (' BasePath ')) EXIT (' No Direct script asscess allowed ‘); Require_once (APPPATH. ' libraries/smarty-2.6.26/libs/smarty.class.php ');     Class Cismarty extends Smarty {protected $ci;         Public Function __construct () {$this->ci = & Get_instance (); $this->ci->load->config (' smarty ');//load Smarty configuration file//Get related configuration item         $this->template_dir = $this->ci->config->item (' Template_dir ');         $this->complie_dir = $this->ci->config->item (' Compile_dir ');         $this->cache_dir = $this->ci->config->item (' Cache_dir ');         $this->config_dir = $this->ci->config->item (' Config_dir ');         $this->template_ext = $this->ci->config->item (' Template_ext ');         $this->caching = $this->ci->config->item (' caching ');     $this->cache_lifetime = $this->ci->config->item (' lefttime '); }} 4, create a new file smarty.php file inside the Config folder of the project directory, the contents are as follows: <?php if (! defined (' BasePath ')) exit (' No Direct script access Allowe d '); $config [' theme '] = ' default '; $config [' template_dir '] = APPPATH. ' Views '; $config [' compile_dir '] = Fcpath. ' Templates_c '; $config [' cache_dir '] = Fcpath. ' Cache '; $config [' config_dir '] = Fcpath. ' Configs '; $config [' template_ext '] = '. html '; $config [' caching '] = False; $config [' lefttime '] = 60; 5. In the directory where the portal file is located, create a new folder Templates_c, Cache, configs, 6, locate the autoload.php file in the Config directory below the project directory to modify the $autoload [' libraries '] = Array ( ' Cismarty ');//The purpose is: to let the system run, automatically loaded, do not think that the controller manually loaded 7, in the core folder of the project directory to create new files my_controller.php content as follows://extension Core control class <?php if (! Defined (' BasePath ')) exit (' No direct access allowed. ');     Class My_controller extends Ci_controller {//The original is wrong here Public function __construct () {parent::__construct ();     The Public function assign ($key, $val) {$this->cismarty->assign ($key, $val);     The Public function display ($html) {$this->cismarty->display ($html); }} configuration is complete--------------------------------------------------------------------------------------------------------- Example of---------------------------------------------use method: In the controller such as: <?php if (! defined (' BasePath ')) exit (' No Direct Script access allowed '); Class Welcome extends My_controller {///The original is wrong here Public Function index () {//$this->load->view (' WElcome_message ');         $data [' title '] = ' title ';         $data [' num '] = ' 123456789 '; $this->cismarty->assign (' data ', $data);         Also $this->assign (' data ', $data);         $this->assign (' tmp ', ' hello '); $this->cismarty->display (' test.html ');     Also $this->display (' test.html '); }} and then in view: The attempt folder is under views of the project directory: New file test.html <! DOCTYPE html> 

  

* * The best way to integrate Smarty with the CI framework of the "CI Framework" PHP

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.