Home >Backend Development >PHP Tutorial >Detailed explanation of how CodeIgniter integrates smarty, codeignitersmarty_PHP tutorial
The example in this article describes the method of integrating smarty with CodeIgniter. Share it with everyone for your reference. The specific steps are as follows:
1. Download smarty
Extract to the libraries directory of ci, such as:
ci/application/libraries/Smarty-2.6.20
2. Write your own class library file Mysmarty.php
The code is as follows:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require "Smarty-2.6.20/libs/Smarty.class.php"; /** * @file system/application/libraries/Mysmarty.php */ class Mysmarty extends Smarty { function Mysmarty() { $this->Smarty(); $config =& get_config(); // absolute path prevents "template not found" errors $this->template_dir = (!empty($config['smarty_template_dir']) ? $config['smarty_template_dir'] : BASEPATH . 'application/views/'); $this->compile_dir = (!empty($config['smarty_compile_dir']) ? $config['smarty_compile_dir'] : BASEPATH . 'cache/'); //use CI's cache folder if (function_exists('site_url')) { // URL helper required $this->assign("site_url", site_url()); // so we can get the full path to CI easily } } /** * @param $resource_name string * @param $params array holds params that will be passed to the template * @desc loads the template */ function view($resource_name, $params = array()) { if (strpos($resource_name, '.') === false) { $resource_name .= '.html'; } if (is_array($params) && count($params)) { foreach ($params as $key => $value) { $this->assign($key, $value); } } // check if the template file exists. if (!is_file($this->template_dir . $resource_name)) { show_error("template: [$resource_name] cannot be found."); } return parent::display($resource_name); } } // END class smarty_library ?>
3. Let ci automatically load smarty in autoload.php
$autoload['libraries'] = array('database', 'mysmarty');
Or load smarty yourself when using a template
$this->load->library("mysmarty");
4.smarty variable assignment display template
$this->mysmarty->assign('test', 'Hello World.'); $this->mysmarty->view('smarty');
Note: external resource files such as images css are placed outside the ci system folder in the root directory of the website
Best to use:
$this->load->helper('url');
base_url() to access:
base_url()."images/xxx.jpg"
Don’t put it in the system
Supplement: The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:
php code online formatting and beautification tool:
http://tools.jb51.net/code/phpformat
In addition, since php belongs to the C language style, the following tool can also format php code:
C language style/HTML/CSS/json code formatting and beautification tool:
http://tools.jb51.net/code/ccode_html_css_json
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "Summary of Common Methods in ThinkPHP", "Introduction Tutorial on Zend FrameWork Framework", "Introduction Tutorial on PHP Object-Oriented Programming", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.