扩展CodeIgniter的MY_Parser,实现页眉页脚自动加载解析
To eliminate repeating header and footer parse calls in every controller, you can extend CodeIgniter's base CI_Parser class with a custom subclass that handles layout rendering automatically. Here's a straightforward implementation:
Step 1: Create the Custom Parser Class
Make a file named MY_Parser.php in your application/core directory. This class will inherit from CI_Parser and add logic to include headers/footers by default:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class MY_Parser extends CI_Parser { protected $CI; public function __construct() { parent::__construct(); $this->CI =& get_instance(); // Access the CI instance to retrieve theme settings } /** * Parse template with automatic header/footer inclusion * @param string $template Path to your main content template * @param array $data Data to pass to all templates * @param bool $return Whether to return output instead of sending to browser * @param bool $include_layout Toggle header/footer (default: true) * @return string|null */ public function parse($template, $data = array(), $return = FALSE, $include_layout = TRUE) { $output = ''; // Add header if layout is enabled if ($include_layout) { $header_path = $this->CI->settings['theme'] . '/header'; $output .= parent::parse($header_path, $data, TRUE); } // Add main content template $output .= parent::parse($template, $data, TRUE); // Add footer if layout is enabled if ($include_layout) { $footer_path = $this->CI->settings['theme'] . '/footer'; $output .= parent::parse($footer_path, $data, TRUE); } // Handle return vs direct output if ($return) { return $output; } $this->CI->output->append_output($output); } }
Step 2: Simplify Controller Code
Now you can replace your three separate parse calls with a single line—header and footer will load automatically:
// Old, repetitive code: // $this->parser->parse($this->settings['theme'].'/header', $data); // $this->parser->parse($this->settings['theme'].'/register', $data); // $this->parser->parse($this->settings['theme'].'/footer', $data); // New, clean code: $this->parser->parse($this->settings['theme'].'/register', $data);
Step 3: Skip Layout for Specific Pages
If you need a page without header/footer (like a modal or API response), pass false as the fourth parameter:
// No header/footer for this page $this->parser->parse($this->settings['theme'].'/modal_content', $data, FALSE, FALSE);
Alternative: Dedicated Layout Method
If you want to keep the original parse() method intact for non-layout templates, add a separate parse_layout() method instead:
// Inside MY_Parser.php public function parse_layout($main_template, $data = array(), $return = FALSE) { $output = ''; // Parse header $header_path = $this->CI->settings['theme'] . '/header'; $output .= parent::parse($header_path, $data, TRUE); // Parse main content $output .= parent::parse($main_template, $data, TRUE); // Parse footer $footer_path = $this->CI->settings['theme'] . '/footer'; $output .= parent::parse($footer_path, $data, TRUE); if ($return) { return $output; } $this->CI->output->append_output($output); }
Usage in controller:
$this->parser->parse_layout($this->settings['theme'].'/register', $data);
Key Notes
- Ensure
$this->settings['theme']is accessible via the CI instance (e.g., loaded in your base controller or as a config item). - CodeIgniter's default subclass prefix is
MY_—if you changed this inconfig/config.php, adjust the class name to match your custom prefix.
内容的提问来源于stack exchange,提问作者Rangka Kacang

