How to integrate smarty with CodeIgniter, codeignitersmarty_PHP tutorial

Source: Internet
Author: User
Codeignitersmarty. CodeIgniter describes how to integrate smarty. This document describes how to integrate smarty with CodeIgniter. For your reference, the specific steps are as follows: 1. download the sm CodeIgniter integration smarty method, codeignitersmarty

This article describes how CodeIgniter integrates smarty. For your reference, follow these steps:

1. download smarty

Decompress the package to the libraries directory of ci, for example:

Ci/application/libraries/Smarty-2.6.20

2. Compile the Mysmarty. php class library file.

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. enable ci to automatically load smarty in autoload. php

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

Or load the smarty manually when using the template.

$this->load->library("mysmarty");

4. smarty variable value display template

$this->mysmarty->assign('test', 'Hello World.');$this->mysmarty->view('smarty');

Note: external resource files such as images css are stored in the root directory of the website outside the ci system folder.

Best use:

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

Base_url () to access:

base_url()."images/xxx.jpg"

Do not put it in system

Supplement: The editor recommends a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:

Php code online formatting and beautification tools:

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

In addition, because 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 tools:
Http://tools.jb51.net/code/ccode_html_css_json

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.