_php Example of CodeIgniter integrated Smarty method

Source: Internet
Author: User
Tags autoload php programming codeigniter zend framework

This article illustrates the method of CodeIgniter integration Smarty. Share to everyone for your reference, the specific steps are as follows:

1. Download Smarty

Extract to Ci's libraries directory as:

ci/application/libraries/smarty-2.6.20

2. Write mysmarty.php's own class library file

The code is as follows:

 <?php if (!defined (' BasePath ')) exit (' No Direct script access allowed '); require "Smar
Ty-2.6.20/libs/smarty.class.php "; /** * @file system/application/libraries/mysmarty.php */class Mysmarty extends Smarty {function mysmarty () {$th
    Is->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_u RL ", Site_url ()); So we can get the full path to CI easily}/** * @param $resource _name String * @param $params array hold s params that'll 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::d isplay ($resource _name); }//End Class Smarty_library?> 

3. In autoload.php let CI automatically load Smarty

$autoload [' libraries '] = Array (' database ', ' mysmarty ');

Or you can load the smarty yourself using the 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 in the site root directory outside the CI system folder

Best use:

$this->load->helper (' url ');

Base_url () Visit:

Base_url (). " Images/xxx.jpg "

Don't put it in system.

Add: Small knitting here recommend a site for the layout of the PHP format landscaping tools to help you in the future of PHP programming code layout:

PHP code online format Landscaping tools:

Http://tools.jb51.net/code/phpformat

In addition, because PHP belongs to the C language style, the following tool can also be used to format PHP code:

C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json

More interested in CodeIgniter related content readers can view the site topics: "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "PHP Excellent Development Framework Summary", "thinkphp Introductory Course", " Thinkphp Common Methods Summary, "Zend Framework Introduction Course", "PHP object-oriented Programming Introduction Course", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on CodeIgniter framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.