搜索
首页php教程php手册Codeigniter 生成静态页面

Codeigniter 生成静态页面

Jun 13, 2016 am 10:55 AM
codeigniter使用生成简单论坛静态页面

    使用CI来生成静态页面,其实很简单,就像论坛里面说的那样,读出页面中的数据,再写入html文件中,最后显示这个html文件就行了,好吧,上码。

 

 

[php] 

                

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);  

        }  

                            

   ......  

    }    

}  

 

              

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);

        }

                          

   ......

    }  

}调用

 

 

[html]  

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

 

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

是否生成HTML文件

 

$config["html"]                =  TRUE;

 

 

 

 

全部代码如下

 

 

[php] 

         

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('=', '

        }  

        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();  

        }  

    }    

}  

 

       

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('=', '

        }

        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!

         */ www.2cto.com

        if (ob_get_level() > $this->_ci_ob_level + 1)

        {

            ob_end_flush();

        }

        else

        {

            $_ci_CI->output->append_output(ob_get_contents());

            @ob_end_clean();

        }

    }  

}

 

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用