Skip to content

Masterpages for CodeIgniter

World Wide Web Server edited this page Jul 4, 2012 · 6 revisions

Category:Library:Views Category:Library:Community

Note: This is a PHP5 library.

application/libraries/MasterPage.php [code] <?php if ( ! defined ( 'BASEPATH' ) ) exit ( 'No direct script access allowed.' ); /**

  • @author Kim Johansson hagbarddenstore@gmail.com

  • @copyright Copyright (c) 2008, Kim Johansson

  • @version 0.0.1 */ class MasterPage { private $masterPage = ''; private $contentPages = array ( ); private $ci = null;

    /**

    • @access public
    • @param string $masterPage Optional file to use as MasterPage. */ public function __construct ( $masterPage = '' ) { $this->CI = get_instance ( ); if ( ! empty ( $masterPage ) ) $this->setMasterPage ( $masterPage ); }

    /**

    • @access public
    • @param string $masterPage File to use as MasterPage. */ public function setMasterPage ( $masterPage ) { // Check if the supplied masterpage exists. if ( ! file_exists ( APPPATH . 'views/' . $masterPage . EXT ) ) throw new Exception ( APPPATH . 'views/' . $masterPage . EXT ); $this->masterPage = $masterPage; }

    /**

    • @access public
    • @return string The current MasterPage. */ public function getMasterPage ( ) { return $this->masterPage; }

    /**

    • @access public
    • @param string $file The view file to add.
    • @param string $tag The tag in the MasterPage it should match.
    • @param mixed $content The content to be used in the view file. */ public function addContentPage ( $file, $tag, $content = array ( ) ) { $this->contentPages[$tag] = $this->CI->load->view ( $file, $content, true ); }

    /**

    • @access public

    • @param array $content Optional content to be added to the MasterPage. */ public function show ( $content = array ( ) ) { // Get the content of the MasterPage and replace all matching tags with their // respective view file content. $masterPage = $this->CI->load->view ( $this->masterPage, $content, true ); foreach ( $this->contentPages as $tag => $content ) { $masterPage = str_replace ( '<mp:' . ucfirst ( strtolower ( $tag ) ) . ' />', $content, $masterPage ); }

      // Finally, print the data. echo $masterPage; } } ?> [/code]

[b]Usage: [/b]

In the controller: [code] class MyController extends Controller { public function __construct ( ) { parent::__construct ( ); $this->load->library ( 'masterpage' ); }

public function index ( ) {
    $this->masterpage->setMasterPage ( 'masterpage_default' );
    // content_index is the view file to use.
    // content is the tag in the masterpage file we want to replace.
    $this->masterpage->addContentPage ( 'content_index', 'content' );

    // Show the masterpage to the world!
    $this->masterpage->show ( );
}

} [/code]

The masterpage: application/views/masterpage_default.php [code] <html> <head> <title></title> </head>

&lt;body&gt;

<!-- MasterPage tags must be capitalized and rest lowercase. --> <mp:Content /> </body> </html> [/code]

The viewfile: application/views/content_index.php [code]

Hello world!

[/code]

That's about it, if you want to know more, just email me at hagbarddenstore at gmail dot com.

Clone this wiki locally