Home > Article > Backend Development > Analysis of methods for CI framework integration with Smarty, CI framework integration with smarty_PHP tutorial
The example in this article describes the method of CI framework integrating Smarty. Share it with everyone for your reference, the details are as follows:
Because the template function that comes with CI is not very convenient, people generally adopt the method of integrating Smarty to make up for the shortcomings of CI.
I have read a lot of tutorials on CI integration with Smarty on the Internet, including a highlight post in our CI forum
http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=10345.
After comparing these tutorials myself, I think the following plan is the best among them all, and I highly recommend it to everyone (of course it is also the plan I adopt myself)
Source:
http://www.cnmiss.cn/?p=261
I have corrected some errors in the original article
Let me talk about the reasons why I think it is better. Comparing this solution with the solution in our forum, you will find that this solution has one more advantage because it extends the core class,
It extends the Smarty class methods assign and display to Ci's controller, so when we use Smarty in CI, we can use it like this:
public function index() { //$this->load->view('welcome_message'); $data['title'] = '标题'; $data['num'] = '123456789'; //$this->cismarty->assign('data',$data); // 也可以 $this->assign('data',$data); $this->assign('tmp','hello'); //$this->cismarty->display('test.html'); // 也可以 $this->display('test.html'); }
Through the simple extension of the core controller class, when everyone uses Smarty in CI, the usage habits are the same as using Smarty directly. This is a great advantage.
And judging from the expansion of the core class library, you can also see that the author of this article has a good understanding of the CI framework.
According to this article, I not only successfully integrated Smaty, but also further strengthened my understanding of CI.
Furthermore, this solution places Smarty’s configuration files in the config directory of the CI framework, and the use of both is very standardized.
Finally achieved "the seamless combination of CI and Smaty".
The following is the specific tutorial: // I made some modifications based on the original text and corrected some errors in the original text. Please note that the places with '//' below are the places that I have modified or modified by myself. Another place to add.
CI version: 2.1.4 // (version used when this article was published)
Smarty version: Smarty-2.6.26 // Because I have used this version before, in order to take care of my own usage habits, I do not use the latest Smaty version here. After everyone understands the expansion principle, you can choose the Smatry version you want to use.
1. Go to the corresponding site to download the source code package of Smarty; // I am using Smarty-2.6.26
2. Copy the libs folder in the source package to the libraries folder under the CI project directory, and rename it to Smarty-2.6.26; //
3. Create a new file Cismarty.php in the libraries folder of the project directory. The contents are as follows:
<?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');//加载smarty的配置文件 //获取相关的配置项 $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 smart.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 new folders templates_c, cache, configs in the directory where the entry file is located;
6. Find the autoload.php file in the config directory under the project directory
Edit this
$autoload['libraries'] = array('Cismarty'); //目的是:让系统运行时,自动加载,不用人为的在控制器中手动加载
7. Create a new file MY_Controller.php in the core folder of the project directory. The content is 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 completed
Usage example:
in the controller like:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Welcome extends MY_Controller { // 原文这里写错 public function index() { //$this->load->view('welcome_message'); $data['title'] = '标题'; $data['num'] = '123456789'; //$this->cismarty->assign('data',$data); // 亦可 $this->assign('data',$data); $this->assign('tmp','hello'); //$this->cismarty->display('test.html'); // 亦可 $this->display('test.html'); } }
Then in the view: the view folder is located under views in the project directory:
New file test.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>{ $test.title}</title> //( 原文是 <title>{$test['title']}</title>,是错误的写法,也有可能是Smarty版本的原因) <style type="text/css"> </style> </head> <body> {$test.num|md5} // 原文这里也写错了 <br> {$tmp} </body> </html>
Readers who are interested in more CodeIgniter-related content can check out the special topics of this site: "Basic Tutorial for Getting Started with Smarty Templates", "Introductory Tutorial for CodeIgniter", "Advanced Tutorial for CI (CodeIgniter) Framework", "Summary of Excellent PHP Development Framework" ", "ThinkPHP introductory tutorial", "ThinkPHP common methods summary", "Zend FrameWork framework introductory tutorial", "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.