CodeIgniter整合smarty的方法詳解,codeignitersmarty_PHP教程

來源:互聯網
上載者:User

CodeIgniter整合smarty的方法詳解,codeignitersmarty


本文執行個體講述了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自動載入smarty

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

或者 使用模板時再自己載入smarty

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

4.smarty變數賦值 display模板

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

註:images css 等外部資源檔 放在ci系統檔案夾外 網站根目錄下

最好用:

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

base_url()來訪問:

base_url()."images/xxx.jpg"

不要放到system裡

補充:小編在這裡推薦一款本站的php格式化美化的排版工具協助大家在以後的PHP程式設計中進行代碼排版:

php代碼線上格式化美化工具:

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

另外,由於php屬於C語言風格,因此下面這款工具同樣可以實現php代碼的格式化:

C語言風格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

更多關於CodeIgniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)架構進階教程》、《php優秀開發架構總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork架構入門教程》、《php物件導向程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於CodeIgniter架構的PHP程式設計有所協助。

http://www.bkjia.com/PHPjc/1133073.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133073.htmlTechArticleCodeIgniter整合smarty的方法詳解,codeignitersmarty 本文執行個體講述了CodeIgniter整合smarty的方法。分享給大家供大家參考,具體步驟如下: 1.下載sm...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.