Codeigniter 產生靜態頁面

來源:互聯網
上載者:User

      使用CI來產生靜態頁面,其實很簡單,就像論壇裡面說的那樣,讀出頁面中的資料,再寫入html檔案中,最後顯示這個html檔案就行了,好吧,上碼。

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');              class MY_Loader extends CI_Loader {                      public function m_view($view, $vars = array(), $return = FALSE){        return $this->_m_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));    }                      protected function _m_ci_load($_ci_data){          .....                                $_ci_html_file=($_ci_ext==='')? $_ci_view.".html" : $_ci_view;//這,產生靜態頁面的檔案名稱                                      foreach ($this->_ci_view_paths as $_ci_view_file => $cascade){                if (file_exists($_ci_view_file.$_ci_file)){                    $_ci_path = $_ci_view_file.$_ci_file;                    $_ci_html_path=$_ci_view_file.$_ci_html_file;//產生靜態頁面的路徑                    $file_exists = TRUE;                    break;                }      ......            }        }                   .......        //在這      if(config_item("html")===TRUE){//是否開啟產生靜態頁面            $_html_file=@fopen($_ci_html_path,'r');//建立.html檔案            $buffer = ob_get_contents();            @ob_end_clean();            if(!$_html_file||(@filesize($_ci_html_path)!=strlen($buffer))){ //如果檔案不存在或檔案已更變                $_html_file=@fopen($_ci_html_path,'w');                flock($_html_file, LOCK_EX);                fwrite($_html_file, $buffer);                                  flock($_html_file, LOCK_UN);                fclose($_html_file);            }            //echo(filesize($_ci_html_path)."-".strlen($buffer));            include($_ci_html_path);        }                             ......    }  }

調用

$this->load->m_view('login',$datas);

是否產生HTML檔案

$config["html"]               
=  TRUE;


全部代碼如下

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');       class MY_Loader extends CI_Loader {               public function m_view($view, $vars = array(), $return = FALSE){        return $this->_m_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));    }               protected function _m_ci_load($_ci_data){        // Set the default data variables        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val){            $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;        }               $file_exists = FALSE;        // Set the path to the requested file        if (is_string($_ci_path) && $_ci_path !== ''){            $_ci_x = explode('/', $_ci_path);//使用一個字串分割另一個字串            $_ci_file = end($_ci_x);//將數組的內部指標指向最後一個單元        }else{            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);// 返迴文件路徑的資訊            $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;            $_ci_html_file=($_ci_ext==='')? $_ci_view.".html" : $_ci_view;//這,產生靜態頁面的檔案名稱                               foreach ($this->_ci_view_paths as $_ci_view_file => $cascade){                if (file_exists($_ci_view_file.$_ci_file)){                    $_ci_path = $_ci_view_file.$_ci_file;                    $_ci_html_path=$_ci_view_file.$_ci_html_file;//產生靜態頁面的路徑                    $file_exists = TRUE;                    break;                }                       if ( ! $cascade){                    break;                }            }        }               if ( ! $file_exists && ! file_exists($_ci_path))        {            show_error('Unable to load the requested file: '.$_ci_file);        }               // This allows anything loaded using $this->load (views, files, etc.)        // to become accessible from within the Controller and Model functions.        $_ci_CI =& get_instance();        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)        {            if ( ! isset($this->$_ci_key))            {                $this->$_ci_key =& $_ci_CI->$_ci_key;            }        }               /*         * Extract and cache variables         *         * You can either set variables using the dedicated $this->load->vars()         * function or via the second parameter of this function. We'll merge         * the two types and cache them so that views that are embedded within         * other views can have access to these variables.         */        if (is_array($_ci_vars))        {            $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);        }        extract($this->_ci_cached_vars);               /*         * Buffer the output         *         * We buffer the output for two reasons:         * 1. Speed. You get a significant speed boost.         * 2. So that the final rendered template can be post-processed by         *  the output class. Why do we need post processing? For one thing,         *  in order to show the elapsed page load time. Unless we can         *  intercept the content right before it's sent to the browser and         *  then stop the timer it won't be accurate.         */        ob_start();               // If the PHP installation does not support short tags we'll        // do a little string replacement, changing the short tags        // to standard PHP echo statements.        if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE            && config_item('rewrite_short_tags') === TRUE && function_usable('eval')        )        {            echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));        }        else        {            include($_ci_path); // include() vs include_once() allows for multiple views with the same name        }               log_message('debug', 'File loaded: '.$_ci_path);               // Return the file data if requested        if ($_ci_return === TRUE)        {            $buffer = ob_get_contents();            @ob_end_clean();            return $buffer;        }        //在這         if(config_item("html")===TRUE){//是否開啟產生靜態頁面            $_html_file=@fopen($_ci_html_path,'r');//建立.html檔案            $buffer = ob_get_contents();            @ob_end_clean();            if(!$_html_file||(@filesize($_ci_html_path)!=strlen($buffer))){                       $_html_file=@fopen($_ci_html_path,'w');                       flock($_html_file, LOCK_EX);                fwrite($_html_file, $buffer);                                  flock($_html_file, LOCK_UN);                fclose($_html_file);            }            //echo(filesize($_ci_html_path)."-".strlen($buffer));            include($_ci_html_path);        }                                  /*         * Flush the buffer... or buff the flusher?         *         * In order to permit views to be nested within         * other views, we need to flush the content back out whenever         * we are beyond the first level of output buffering so that         * it can be seen and included properly by the first included         * template and any subsequent ones. Oy!         */        if (ob_get_level() > $this->_ci_ob_level + 1)        {            ob_end_flush();        }        else        {            $_ci_CI->output->append_output(ob_get_contents());            @ob_end_clean();        }    }  }

來源:http://www.xiuxiandou.com/blog-33.html

-----------廣告區
休閑豆,IT資訊,IT新聞資訊,電影BT下載,高畫質 DVD下載,電影下載,單機遊戲下載,遊戲下載,電子書下載,電子書PDF下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.