CI框架視圖繼承
阿新 • • 發佈:2018-09-03
views var fin 沒有 his item 數組 ews cached
CI(CodeIgniter)框架 視圖繼承
這個代碼不是我擼的 ... 當時在哪兒找的忘了 ... 如果有侵權什麽的 ... 聯系我刪了 ...
需要去core裏面創建一個MY_loader.php 然後直接用就可以了
<?php if ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘); /** * Loader Extend Class * * Loads views and files * */ class MY_Loader extends CI_Loader { /** * 視圖堆棧 * * @var array*/ private $_stacks = array(); /** * 當前處理的視圖 * * @var int */ private $_current; /** * 視圖的繼承 * * @param string $tplname * * @access protected */ protected function _extends($tplname) { $this->_stacks[$this->_current][‘extends‘] = $tplname; } /** * 開始定義一個區塊 * * @param string $block_name * @param mixed $config * * @access protected */ protected function _block($block_name, $config = null) { $stack =& $this->_stacks[$this->_current]; if (!empty($stack[‘blocks_stacks‘])) {// 如果存在嵌套的 block,則需要記錄下嵌套的關系 $last = $stack[‘blocks_stacks‘][count($stack[‘blocks_stacks‘]) - 1]; $stack[‘nested_blocks‘][] = array($block_name, $last); } $this->_stacks[$this->_current][‘blocks_config‘][$block_name] = $config; array_push($stack[‘blocks_stacks‘], $block_name); ob_start(); } /** * 結束一個區塊 * * @access protected */ protected function _endblock() { $block_name = array_pop($this->_stacks[$this->_current][‘blocks_stacks‘]); $this->_stacks[$this->_current][‘blocks‘][$block_name] = ob_get_clean(); echo "%block_{$block_name}_{$this->_stacks[$this->_current][‘id‘]}%"; } /** * 載入一個視圖片段 * * @param string $element_name 視圖名稱 * @param array $vars * * @access protected */ protected function _element($element_name, array $vars = null) { $file_exists = FALSE; $filename = ‘‘; foreach ($this->_ci_view_paths as $view_file => $cascade) { // $filename = $view_file.$element_name.EXT; $filename = $view_file.$element_name; if ( ! file_exists($filename)) { $file_exists = FALSE; }else{ $file_exists = TRUE;break; } } if(!$file_exists){ show_error(‘Unable to load the requested file: ‘.$filename); }else{ extract($this->_ci_cached_vars); if (is_array($vars)) extract($vars); include($filename); } } /** * Loader * * 這個函數用來加載視圖或者文件. * 這個函數改寫 CI_Loader 類內函數,使其支持視圖繼承和多重繼承。 * * @access private * @param array * @return void */ function _ci_load($_ci_data) { // 設置默認的數據變量 foreach (array(‘_ci_view‘, ‘_ci_vars‘, ‘_ci_path‘, ‘_ci_return‘, ‘_ci_viewid‘, ‘_ci_stack‘) as $_ci_val) { $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val]; } // 設置請求文件的路徑 if ($_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; foreach ($this->_ci_view_paths as $view_file => $cascade) { if (file_exists($view_file.$_ci_file)) { $_ci_path = $view_file.$_ci_file; $file_exists = TRUE; break; } if ( ! $cascade) { break; } } } if ( ! file_exists($_ci_path)) { show_error(‘Unable to load the requested file: ‘.$_ci_file); } // 這允許任何加載使用 $this->load (views, files, etc.) // 成為從內部控制器和模型函數訪問. $_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; } } /* * 提取緩存和變量 也就是把數組下標變成變量 * */ if (is_array($_ci_vars)) { $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); } extract($this->_ci_cached_vars); if ( ! $_ci_viewid) $_ci_viewid = mt_rand(); $stack = array( ‘id‘ => $_ci_viewid, ‘contents‘ => ‘‘, ‘extends‘ => ‘‘, ‘blocks_stacks‘ => array(), ‘blocks‘ => array(), ‘blocks_config‘ => array(), ‘nested_blocks‘ => array(), ); array_push($this->_stacks, $stack); $this->_current = count($this->_stacks) - 1; unset($stack); ob_start(); if ((bool) @ini_get(‘short_open_tag‘) === FALSE AND config_item(‘rewrite_short_tags‘) == TRUE) { 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); $stack = $this->_stacks[$this->_current]; $stack[‘contents‘] = ob_get_clean(); // 如果有繼承視圖,則用繼承視圖中定義的塊內容替換當前視圖的塊內容 if (is_array($_ci_stack)) { foreach ($_ci_stack[‘blocks‘] as $block_name => $contents) { if (isset($stack[‘blocks_config‘][$block_name])) { switch (strtolower($stack[‘blocks_config‘][$block_name])) { case ‘append‘: $stack[‘blocks‘][$block_name] .= $contents; break; case ‘replace‘: default: $stack[‘blocks‘][$block_name] = $contents; } } else { $stack[‘blocks‘][$block_name] = $contents; } } } // 如果有嵌套 block,則替換內容 while (list($child, $parent) = array_pop($stack[‘nested_blocks‘])) { $stack[‘blocks‘][$parent] = str_replace("%block_{$child}_{$_ci_viewid}%", $stack[‘blocks‘][$child], $stack[‘blocks‘][$parent]); unset($stack[‘blocks‘][$child]); } // 保存對當前視圖堆棧的修改 $this->_stacks[$this->_current] = $stack; if ($stack[‘extends‘]) { //私有繼承. // $filename = "{$stack[‘extends‘]}".EXT; $filename = "{$stack[‘extends‘]}"; return $this->_ci_load(array( ‘_ci_view‘ => $filename, //‘_ci_vars‘ => $this->_ci_cached_vars, ‘_ci_return‘ => $_ci_return, ‘_ci_viewid‘=>$_ci_viewid, ‘_ci_stack‘=>$this->_stacks[$this->_current], )); } else { // 最後一個視圖一定是沒有 extends 的 $last = array_pop($this->_stacks); foreach ($last[‘blocks‘] as $block_name => $contents) { $last[‘contents‘] = str_replace("%block_{$block_name}_{$last[‘id‘]}%", $contents, $last[‘contents‘]); } $this->_stacks = array(); if ($_ci_return === TRUE) { @ob_end_clean(); return $last[‘contents‘]; } if (ob_get_level() > $this->_ci_ob_level + 1) { ob_end_flush(); } else { $_ci_CI->output->append_output($last[‘contents‘]); @ob_end_clean(); } } } }
修改了一點點 ... 其中有後綴EXT
但是沒法加載 .. 說找不到文件 所以就去掉了
使用就很簡單了 :
<?php $this->_extends(‘xxx‘); ?> <?php $this->_block(‘xxx‘);?> <?php $this->_endblock(); ?>
CI框架視圖繼承