Integration of the CI framework with the Smarty method analysis, integration of the ci framework with the smarty_PHP tutorial

Source: Internet
Author: User
The CI framework integrates Smarty method analysis, and the ci framework integrates smarty. The CI framework integrates the Smarty method analysis. the ci framework integrates smarty. This article describes how to integrate the CI framework with Smarty. This article is for your reference. The details are as follows: Because the CI built-in template CI framework integrates the Smarty method analysis, the ci framework integrates the smarty

This article describes how to integrate Smarty with the CI framework. We will share this with you for your reference. The details are as follows:

Because the built-in template function of CI is not very convenient, it is widely used to integrate Smarty to make up for the shortcomings of CI.

I have read a lot of CI integration Smarty tutorials on the Internet, including an excellent post in our CI Forum.

Http://codeigniter.org.cn/forums/forum.php? Mod = viewthread & tid = 10345.

I have compared these tutorials. I think the following solution is the best in all of them and I strongly recommend it to you (of course, it is also my own solution)

Source:

Http://www.cnmiss.cn /? P = 261

I have fixed some errors in the original article.

Next, let's talk about the reason why I think it is better. by comparing this solution with our Forum solution, you will find that this solution has extended the core class,

It extends the Smarty class methods assign and display to the Ci controller, so we can use Smarty in CI as follows:

Public function index () {// $ this-> load-> view ('Welcome _ message'); $ data ['title'] = 'title '; $ data ['num'] = '000000'; // $ 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 simple extension of the core controller class, the usage habits of using Smary in CI are the same as that of using Smarty directly. this is a great advantage.

From the extension of the core class library, we can also see that the author's understanding of the CI framework is very good.

According to this article, I not only successfully integrated Smaty, but also further enhanced my understanding of CI.

In addition, this solution places the Smarty configuration file under the config Directory of the CI framework, and uses both of them in a standard way.

Finally, "seamless integration of CI and Smaty" is achieved ".

The following is a specific tutorial: // I made some modifications on the basis of the original text, corrected some errors in the original text. Note that '//' is included in the following section, it is a place I have modified or added myself.

CI version: 2.1.4 // (version used in this article)

Smarty version: Smarty-2.6.26 // because I used this version earlier, in order to take care of my usage habits, the latest Smaty version is not used here. everyone understands the extension principle, you can select the desired Smatry version.

1. download the Smarty source code package from the corresponding site. // Here I use Smarty-2.6.26.

2. copy the libs folder in the source code package to the libraries folder under the CI project directory and rename it to Smarty-2.6.26 ;//

3. create the Cismarty. php file in the libraries folder of the project directory. the content is 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 the smarty configuration file // Obtain the 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 the smarty. php file in the config folder of the project directory. the content is as follows:

<?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 folder templates_c, cache, and configs in the directory where the file is located;

6. find the autoload. php file in the config directory under the Project Directory.
Modify this

$ Autoload ['libraries'] = array ('cismarty '); // The objective is to enable automatic loading during system running without manual loading in the controller.

7. create the MY_Controller.php file in the core folder of the project directory as follows: // extend the core control class.

<? Php if (! Defined ('basepath') exit ('no direct access allowed. '); class MY_Controller extends CI_Controller {// 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

Example:

Such:

<? Php if (! Defined ('basepath') exit ('no direct script access allowed'); class Welcome extends MY_Controller {// public function index () is incorrect in the original text () {// $ this-> load-> view ('Welcome _ message'); $ data ['title'] = 'title '; $ data ['num'] = '000000'; // $ 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 ');}}

Then, as shown in the figure below: try to place the folder under the views under the Project Directory:

New file test.html

 {$ Test. title}// (The original is{$ Test ['title']}It is a wrong way of writing, or it may be because of the Smarty version){$ Test. num | md5} // The original text is also incorrect.
{$ Tmp}

Related Article

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.