1. Create Smarty class library
1. Copy smarty’s libs file to libraries (I renamed it to smarty here)
2. Create a new Cismarty.php file. (Conforming to file specifications, the first letter of the file name and the first letter of the class name are capitalized, but when the controller reference is loaded, the class name/file name does not need to be capitalized)
Cismarty.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require(APPPATH . 'libraries/smarty/Smarty.class.php'); //CI,文件系统全用相对路径相对index.php所在的路径,url全部用绝对路径。 //BASEPATH - The full server path to the "system" folder //APPPATH - The full server path to the "application" folder class Cismarty extends Smarty { public function __construct() { parent::__construct(); $this->caching = false; $this->setTemplateDir(APPPATH . 'views/Smarty/templates'); //设定所有模板文件都需要放置的目录地址。 $this->setConfigDir(APPPATH . 'views/Smarty/configs'); //设定用于存放模板特殊配置文件的目录, $this->setCacheDir(APPPATH . 'views/Smarty/cache'); //在启动缓存特性的情况下,这个属性所指定的目录中放置Smarty缓存的所有模板 $this->setPluginsDir(APPPATH . 'views/Smarty/plugins'); //插件目录 $this->setCompileDir(APPPATH . 'views/Smarty/templates_c'); //设定Smarty编译过的所有模板文件的存放目录地址 } } ?>
Create a new smarty folder in the corresponding directory. templates,configs,cache,plugins,templates_c.
2. Controller file
Create the controller file paper.php (the first letter of the class name is capitalized) (the constructor function is executed by default when using load to load libraries, and the constructor function and the default index method are executed when using url routing to access the controller.)
paper.php:
<?php class Paper extends CI_Controller { function __construct() { parent::__construct(); } public function pri_body() { $this->load->library('cismarty'); $this->cismarty->assign("name", 1200); $this->cismarty->display('dd.tpl'); } } ?>
You can also configure automatic loading of resources in application/config/autoload.php.