(Local development environment: CI 3.x + Smarty 3.x)
Local development environment preparations
Without further explanation, you can download a local INTEGRATED development environment (XAMPP or WAMPServer) and install it. This document is based on the MAC and XAMPP environment.
First, build CI framework environment
1. Download CI 3.x
Download address: codeigniter.org.cn/download
2, decompression, copy, initial access
Unzip the file and copy the application, System, and index.php folders into your Web root directory to use the CI framework. As for other folders and files, choose whether to copy them together depending on your mood.
Then visit your Web to test: http://localhost/xxx/. If you get the following screen, it indicates that the CI framework environment has been built successfully.
Integrate the Smarty template engine
1. Download Smarty 3.x
Download: github.com/smarty-php/…
2, decompression
3. Create smarty-3.1.30 under application/third_party/ and copy the libs folder from the smarty library to smarty-3.1.30
Create smarty. PHP from application/config where path and delimiter are optional.
<? phpif(! 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; // Subdirectory variable (whether subdirectories are generated in the cache folder)$config['left_delimiter'] = '< {';
$config['right_delimiter'] = '} > ';
Copy the code
5. Create a ci_smarty. PHP file under application/ Libraries (note: the file name can be saved at will, but it will be used in the following steps, so please be case sensitive).
<? phpif(! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH .'third_party/smarty - 3.1.30 / libs/SmartyBC class. PHP');
class Ci_smarty extends SmartyBC {
protected $ci;
public function __construct(){
parent::__construct();
$this->ci = & get_instance();
$this->ci->load->config('smarty'); // Load the smarty configuration file$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'); }}Copy the code
Create a new my_controller.php file under application/core.
<? php class MY_Controller extends CI_Controller { publicfunction __construct() {
parent::__construct();
$this->load->library("Ci_smarty");
}
public function assign($key.$val) {
$this->ci_smarty->assign($key.$val);
}
public function display($html) {
$this->ci_smarty->display($html); }}Copy the code
7. Test Smarty integration
Create a controller file named test.php in the application/controller/ directory as shown in the following code:
<? php /** * Created by PhpStorm. * User: joker * Date: 2017/11/12 * Time: 15:15 */ class Test extends MY_Controller { publicfunction __construct(){
parent::__construct();
}
public function index() {$data = [];
$data['describe'] = 'Test CI framework integration with Smarty Template engine!! ';
$this->assign('data'.$data);
$this->display(APPPATH . 'views/templates/test.tpl'); }}Copy the code
In the application/views/templates/directory to create a controller file test. The TPL, code samples are as follows:
<! DOCTYPE HTML >< HTML >< head> <title> Test smarty integration </title> </head> <body> <h1><{$data["describe"]}></h1>
</body>
</html>
Copy the code
In application/config/routes. PHP file access route of the controller, you can visit the test; If the following interface is obtained, the integration is successful:
This document is integrated with the resources of all the great gods on the Internet, the specific source of resources has been forgotten, if you find any infringement or other behavior, please contact me, I will deal with it, thank you ~~~.