程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> CI Smarty結合

CI Smarty結合

編輯:PHP綜合
      CI全稱CodeIgniter,是一套優雅的PHP框架,主要是MVC設計,在開發中容易上手,也很好用,初學CI,感覺框架這個東西還是蠻方便的,給PHP程序員一個格式,這樣就減少了代碼的個性化和混亂,便於維護。讓我不得不拜服的是這個框架的創始人是一個搖滾樂手,業余時間寫出來的。Smarty是一個PHP模板,是頁面設計和後台邏輯分離開來,便於頁面設計工作和後台程序開發並行,更專業化。
      因為是初學者,也說不出個所以然,這裡主要記錄一下CI 和Smarty結合的操作流程
      1,http://codeigniter.org.cn/下載完整的CI代碼,配置好CI,具體就不詳說,這裡附帶的就是CI手冊是個好東西,很詳細,還有論壇,能幫助初學者解決很多問題。
      2,http://www.smarty.Net/download.PHP下載完整的Smarty代碼,
      3,建立文件夾/applicaton/librarIEs/Smarty/
      4,將講smarty的LIB庫下的文件解壓到上面建立的文件夾下
      5,CI根目錄下Index.PHP中加上(好像默認是有的)
     if (is_dir($application_folder))
    {
    define('APPPATH', $application_folder.'/');
    }
   else
    {
    if ($application_folder == '')
    {
        $application_folder = 'application';
    }
    define('APPPATH', BASEPATH.$application_folder.'/');
    }
6,/applicaton/librarIEs/下建立一個Mysmarty.PHP類
<?php if (!defined('BASEPATH')) exit('No direct script Access allowed');require "Smarty/Smarty.class.PHP";/**
* @file system/application/librarIEs/Mysmarty.PHP
*/
class Mysmarty extends Smarty
{
    protected $_layout_ = "default.tpl";    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']
                    : APPPATH . 'vIEws/');
        $this->compile_dir  = (!empty($config['smarty_compile_dir']) ? $config['smarty_compile_dir']
                    : BASEPATH . 'cache/'); //use CI's cache folder
        $this->plugins_dir[] = (!empty($config['smarty_plugins_dir']) ? $config['smarty_plugins_dir']
                    : APPPATH . 'smarty_plugins/');   
        $this->left_delimiter='<{';
        $this->right_delimiter='}>';                 
    }
    /**
    * @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 .= '.tpl';
        }        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('layouts/' . $layout);
    }
在Controller_mytestController.PHP中寫
$this->load->library('Mysmarty');
$this->mysmarty->vIEw($tpl,$data);//$tpl是你的模板,$data是模板數據smarty模板中的就參照smarty的寫法了
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved