CodeIgniter集成smarty的方法詳解
來源:易賢網(wǎng) 閱讀:807 次 日期:2016-08-18 16:24:06
溫馨提示:易賢網(wǎng)小編為您整理了“CodeIgniter集成smarty的方法詳解”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了CodeIgniter集成smarty的方法。分享給大家供大家參考,具體步驟如下:

1.下載smarty

解壓到ci的libraries目錄 如:

ci/application/libraries/Smarty-2.6.20

2.編寫Mysmarty.php 自己的類庫文件

代碼如下:

<?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.在autoload.php讓ci自動(dòng)加載smarty

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

或者 使用模板時(shí)再自己加載smarty

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

4.smarty變量賦值 display模板

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

$this->mysmarty->view('smarty');

注:images css 等外部資源文件 放在ci系統(tǒng)文件夾外 網(wǎng)站根目錄下

最好用:

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

base_url()來訪問:

base_url()."images/xxx.jpg"

不要放到system里

希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:CodeIgniter集成smarty的方法詳解
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)