forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
Masterpages for CodeIgniter
Derek Jones edited this page Jul 5, 2012
·
6 revisions
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 <[email protected]>
* @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;
}
}
?>
**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.