This article mainly introduces the basic configuration of using Smarty3 in CodeIgniter. This article describes how to create a class library and how to call an instance in the controller, so that you can use Smarty completely. For more information, see
This article mainly introduces the basic configuration of using Smarty3 in CodeIgniter. This article describes how to create a class library and how to call an instance in the controller, so that you can use Smarty completely. For more information, see
1. Create a Smarty class library
1. Copy the smarty libs file to libraries (here I will rename it as smarty)
2. Create a Cismarty. php file. (In accordance with the file specifications, the first letter of the file name and the first letter of the class name are capitalized, but the class name/file name does not need to be capitalized when the Controller references loading)
Cismarty. php
<? Phpif (! Defined ('basepath') exit ('no direct script access allowed'); require (APPPATH. 'libraries/smarty/Smarty. class. php '); // CI, the file system uses the relative path relative to index. php path. All URLs use absolute paths. // BASEPATH-The full server path to the "system" folder // APPPATH-The full server path to the "application" folderclass Cismarty extends Smarty {public function _ construct () {parent ::__ construct (); $ this-> caching = false; $ this-> setTemplateDir (APPPATH. 'Views/Smarty/templates'); // you can specify the directory address for all template files. $ This-> setConfigDir (APPPATH. 'Views/Smarty/configs'); // sets the directory for storing the special configuration files of the template, $ this-> setCacheDir (APPPATH. 'Views/Smarty/cache'); // when the cache feature is enabled, all templates of the Smarty cache are placed in the directory specified by this attribute $ this-> setPluginsDir (APPPATH. 'Views/Smarty/INS ins'); // plug-in directory $ this-> setCompileDir (APPPATH. 'Views/Smarty/templates_c '); // sets the directory where all template files compiled by Smarty are stored.}?>
Create a folder named smarty in the corresponding directory. Templates, configs, cache, plugins, templates_c.
Ii. 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 libraries is loaded using load, and the constructor function and default index method are executed when the controller is accessed using url routing .)
Paper. php:
<? Phpclass 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 resource loading in application/config/autoload. php.
,