CI Framework integrated Smarty Method Analysis, CI framework integration smarty_php Tutorial

Source: Internet
Author: User
Tags autoload codeigniter smarty template zend framework

CI Framework integrated Smarty Method Analysis, CI framework integration Smarty


This paper describes the method of CI framework integrated smarty. Share to everyone for your reference, as follows:

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 in the online tutorial, 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 plan)

Source:

http://www.cnmiss.cn/?p=261

Some of the mistakes in the original text, I have made a correction in this article

The following is the reason why I think it is more excellent, compared to this program and the plan of our forum, you will find that this program is a little more than it expands the core class,

It extends the Smarty class method assign and display to the controller of CI, so we can use it like this when we use Smarty in CI:

Public Function Index () {    //$this->load->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 ');}

Through the simple extension of the core controller class, we use the smary in CI and the usage of smarty directly is the same, this is a great advantage ah.

And from the expansion of the core class library, we 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 have made some changes on the basis of the original text, correcting some of the mistakes in the original text note that there are '//' places that I have modified myself, or where I have added myself.

CI version: 2.1.4//(version used when this article was published)

Smarty version: Smarty-2.6.26//Because I used this version earlier, 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 the Smatry version you want to use.

1, to the corresponding site download Smarty source package;//I use Smarty-2.6.26 here.

2. Copy the Libs folder inside the source package into the Libraries folder under the CI project directory and rename it to smarty-2.6.26;//

3. Create a new file in the Libraries folder of the project directory cismarty.php, the contents are as follows:

<?phpif (!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 within the Config folder of the project directory with the following contents:

<?php if (! defined (' BasePath ')) exit (' No Direct script access allowed '); $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. Create a new folder in the directory where the portal file is located templates_c, cache, configs;

6. Locate the autoload.php file in the Config directory below the project directory
Modify this

$autoload [' libraries '] = Array (' Cismarty ');//The purpose is to allow the system to load automatically when it is run, without manually loading it in the controller

7. Create a new file in the core folder of the project directory my_controller.php content as follows://extended 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 ();  Public  function Assign ($key, $val) {    $this->cismarty->assign ($key, $val);  }  Public function display ($html) {    $this->cismarty->display ($html);}  }

Configuration complete

To use a method instance:

In the controller such as:

<?php if (! defined (' BasePath ')) exit (' No Direct script access allowed '); class Welcome extends My_controller {//original here Write wrong 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 ');}  }

Then in view: The attempt folder is located under views of the project directory:

New File test.html

 
  {$test. Title}{$test [' title ']}, is the wrong wording, it may be the cause of the Smarty version)  {$test. NUM|MD5}//The original is also written in the wrong place
{$tmp}

More interested in CodeIgniter related content readers can view this site topic: "Smarty Template Primer Basic Tutorial", "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "PHP Excellent Development Framework Summary", " thinkphp Introductory Tutorial, "Summary of common methods of thinkphp", "Introduction to the Zend Framework Framework", "Introduction to PHP Object-oriented Programming", "Introduction to Php+mysql Database Operations" and "PHP Common Database Operations Skills Summary"

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

http://www.bkjia.com/PHPjc/1127873.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127873.html techarticle CI Framework Integrated Smarty Method Analysis, CI framework Integration Smarty This paper describes the CI framework integrated Smarty method. Share to everyone for your reference, as follows: Because the CI comes with the template ...

  • 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.