使用CI来生成静态页面,其实很简单,就像论坛里面说的那样,读出页面中的数据,再写入html文件中,最后显示这个html文件就行了,好吧,上码。
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 2 3class MY_Loader extends CI_Loader { 4 5 public function m_view($view, $vars = array(), $return = FALSE){ 6 return $this->_m_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return)); 7 } 8 9 protected function _m_ci_load($_ci_data){ 10 ..... 11 12 $_ci_html_file=($_ci_ext==='')? $_ci_view.".html" : $_ci_view;//这,生成静态页面的文件名 13 14 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade){ 15 if (file_exists($_ci_view_file.$_ci_file)){ 16 $_ci_path = $_ci_view_file.$_ci_file; 17 $_ci_html_path=$_ci_view_file.$_ci_html_file;//生成静态页面的路径 18 $file_exists = TRUE; 19 break; 20 } 21 ...... 22 } 23 } 24 25 ....... 26 //在这 27 if(config_item("html")===TRUE){//是否开启生成静态页面 28 $_html_file=@fopen($_ci_html_path,'r');//创建.html文件 29 $buffer = ob_get_contents(); 30 @ob_end_clean(); 31 if(!$_html_file||(@filesize($_ci_html_path)!=strlen($buffer))){ //如果文件不存在或文件已更变 32 $_html_file=@fopen($_ci_html_path,'w'); 33 flock($_html_file, LOCK_EX); 34 fwrite($_html_file, $buffer); 35 flock($_html_file, LOCK_UN); 36 fclose($_html_file); 37 } 38 //echo(filesize($_ci_html_path)."-".strlen($buffer)); 39 include($_ci_html_path); 40 } 41 42 ...... 43 } 44}
调用
1$this->load->m_view('login',$datas); 2 3$config["html"] = TRUE;
全部代码如下
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 2 3class MY_Loader extends CI_Loader { 4 5 public function m_view($view, $vars = array(), $return = FALSE){ 6 return $this->_m_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return)); 7 } 8 9 protected function _m_ci_load($_ci_data){ 10 // Set the default data variables 11 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val){ 12 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE; 13 } 14 15 $file_exists = FALSE; 16 // Set the path to the requested file 17 if (is_string($_ci_path) && $_ci_path !== ''){ 18 $_ci_x = explode('/', $_ci_path);//使用一个字符串分割另一个字符串 19 $_ci_file = end($_ci_x);//将数组的内部指针指向最后一个单元 20 }else{ 21 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);// 返回文件路径的信息 22 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view; 23 $_ci_html_file=($_ci_ext==='')? $_ci_view.".html" : $_ci_view;//这,生成静态页面的文件名 24 25 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade){ 26 if (file_exists($_ci_view_file.$_ci_file)){ 27 $_ci_path = $_ci_view_file.$_ci_file; 28 $_ci_html_path=$_ci_view_file.$_ci_html_file;//生成静态页面的路径 29 $file_exists = TRUE; 30 break; 31 } 32 33 if ( ! $cascade){ 34 break; 35 } 36 } 37 } 38 39 if ( ! $file_exists && ! file_exists($_ci_path)) 40 { 41 show_error('Unable to load the requested file: '.$_ci_file); 42 } 43 44 // This allows anything loaded using $this->load (views, files, etc.) 45 // to become accessible from within the Controller and Model functions. 46 $_ci_CI =& get_instance(); 47 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var) 48 { 49 if ( ! isset($this->$_ci_key)) 50 { 51 $this->$_ci_key =& $_ci_CI->$_ci_key; 52 } 53 } 54 55 /* 56 * Extract and cache variables 57 * 58 * You can either set variables using the dedicated $this->load->vars() 59 * function or via the second parameter of this function. We'll merge 60 * the two types and cache them so that views that are embedded within 61 * other views can have access to these variables. 62 */ 63 if (is_array($_ci_vars)) 64 { 65 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); 66 } 67 extract($this->_ci_cached_vars); 68 69 /* 70 * Buffer the output 71 * 72 * We buffer the output for two reasons: 73 * 1. Speed. You get a significant speed boost. 74 * 2. So that the final rendered template can be post-processed by 75 * the output class. Why do we need post processing? For one thing, 76 * in order to show the elapsed page load time. Unless we can 77 * intercept the content right before it's sent to the browser and 78 * then stop the timer it won't be accurate. 79 */ 80 ob_start(); 81 82 // If the PHP installation does not support short tags we'll 83 // do a little string replacement, changing the short tags 84 // to standard PHP echo statements. 85 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE 86 && config_item('rewrite_short_tags') === TRUE && function_usable('eval') 87 ) 88 { 89 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path)))); 90 } 91 else 92 { 93 include($_ci_path); // include() vs include_once() allows for multiple views with the same name 94 } 95 96 log_message('debug', 'File loaded: '.$_ci_path); 97 98 // Return the file data if requested 99 if ($_ci_return === TRUE) 100 { 101 $buffer = ob_get_contents(); 102 @ob_end_clean(); 103 return $buffer; 104 } 105 //在这 106 if(config_item("html")===TRUE){//是否开启生成静态页面 107 $_html_file=@fopen($_ci_html_path,'r');//创建.html文件 108 $buffer = ob_get_contents(); 109 @ob_end_clean(); 110 if(!$_html_file||(@filesize($_ci_html_path)!=strlen($buffer))){ 111 $_html_file=@fopen($_ci_html_path,'w'); 112 flock($_html_file, LOCK_EX); 113 fwrite($_html_file, $buffer); 114 flock($_html_file, LOCK_UN); 115 fclose($_html_file); 116 } 117 //echo(filesize($_ci_html_path)."-".strlen($buffer)); 118 include($_ci_html_path); 119 } 120 121 122 /* 123 * Flush the buffer... or buff the flusher? 124 * 125 * In order to permit views to be nested within 126 * other views, we need to flush the content back out whenever 127 * we are beyond the first level of output buffering so that 128 * it can be seen and included properly by the first included 129 * template and any subsequent ones. Oy! 130 */ 131 if (ob_get_level() > $this->_ci_ob_level + 1) 132 { 133 ob_end_flush(); 134 } 135 else 136 { 137 $_ci_CI->output->append_output(ob_get_contents()); 138 @ob_end_clean(); 139 } 140 } 141}
来源地址:生成静态页面
-----------广告区
休闲豆,IT资讯,IT新闻资讯,电影BT下载,高清电影下载,电影下载,单机游戏下载,游戏下载,电子书下载,电子书PDF下载