Home  >  Article  >  Backend Development  >  How to integrate Smarty using CodeIgniter

How to integrate Smarty using CodeIgniter

不言
不言Original
2018-06-14 15:34:591432browse

This article mainly introduces the method of integrating Smarty with CodeIgniter, and analyzes the steps and related setting skills of CodeIgniter3.0.3 integrating Smarty3.1.27 with examples. Friends in need can refer to the following

The examples in this article describe How CodeIgniter integrates Smarty. Share it with everyone for your reference, the details are as follows:

After the release of CI3.0.2, I feel that the template class is still not very easy to use, and it cannot be compiled. Smarty has powerful functions. Once you get used to Smarty tags, it is generally difficult to give up. Moreover, it can compile files and execute them quickly. We can integrate them to make up for the lack of template functions of CI. We are integrating CI version 3.0.3 and Smarty version 3.1.27. The integration process is described below.

1. Download smarty-3.1.27

2. Unzip smarty-3.1.27 to application\libraries in the CI project and delete other files.

3. Create the Ci_smarty.php file in the application\libraries directory with the following code:

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');//加载smarty的配置文件
 $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. Create the configuration file smarty.php in the application\config directory with the following code:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['cache_lifetime'] = 60;
$config['caching'] = false;
$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; //子目录变量(是否在缓存文件夹中生成子目录)
$config['left_delimiter'] = '{';
$config['right_delimiter'] = '}';

5. Create MY_controller.php in application\core. The code is as follows:

class MY_controller extends CI_Controller {
 public function __construct() {
 parent::__construct();
 }
 public function assign($key,$val)
 {
 $this->ci_smarty->assign($key,$val);
 }
 public function display($html)
 {
 $this->ci_smarty->display($html);
 }
}

At this point, the configuration integration work is over. Next, we need to verify whether the configuration is successful.

7. Modify the Welcome.php of application\controllers, 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 配置成功';
 $this->assign('test',$test);
 $this->display('test.html');
 }
}

Then, create the test.html file under application\views, the code is as follows:

{$test}

Enter in the browser address bar: http://localhost/index.php/Welcome

The result shows:

ci 3.0.3 + smarty 3.1.27 配置成功

You’re done!

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About analysis of methods for integrating smarty and adodb in Codeigniter

About Symfony and CodeIgniter for PHP Framework’s Nginx rewrite rule configuration

The above is the detailed content of How to integrate Smarty using CodeIgniter. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn