Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by Hagbarddenstore )

Masterpages for CodeIgniter

Category:Library:Views Category:Library:Community

Note: This is a PHP5 library.

application/libraries/MasterPage.php

<?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$contenttrue );
    
}

    
/**
     * @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$contenttrue );
        foreach ( 
$this->contentPages as $tag => $content {
            $masterPage 
str_replace '<mp:' ucfirst strtolower $tag ) ) . ' />',
            
$content$masterPage );
        
}

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

Usage:

In the controller:

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

The masterpage:
application/views/masterpage_default.php

<html>
    <
head>
        <
title></title>
    </
head>

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

The viewfile:
application/views/content_index.php

<h1>Hello world!</h1

That’s about it, if you want to know more, just email me at hagbarddenstore _at_ gmail _dot_ com.