-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Advanced Layout Library
Derek Jones edited this page Jul 5, 2012
·
5 revisions
Example Usage:
class Page extends MY_Controller {
function index(){
$this->layout->build('page/index', array(
'var1' => 'value1',
'var2' => 'value2'
));
}
}
class MY_Controller extends CI_Controller {
function __construct(){
parent::__construct();
$this->load_layout();
}
function load_layout(){
$this->load->library('layout');
/* Prepend & Append Views (optional) */
$this->layout->prepend('header');
$this->layout->append('footer');
/* Set Global Data (optional) */
$this->layout->set(array(
'global_var1' => 'value1',
'global_var2' => 'value2'
));
/* Add Callback for Formatting Layout (optional) */
function format_html($html){
return str_replace('<b>', '<strong>', $html);
}
$this->layout->set_callback('format_html');
}
}
/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */
Library File: ./application/libraries/layout.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Layout {
var $append_views = array();
var $callback = '';
var $data = array();
var $prepend_views = array();
function __construct(){
$this->ci =& get_instance();
}
function append($view, $data = null){
if (!empty($view)){
$this->append_views[] = $view;
$this->set($data);
}
}
function build($view, $data = null, $no_append = false){
$html = '';
if (!empty($view)){
$this->set($data);
if (!$no_append){
if (count($this->prepend_views)){
foreach ($this->prepend_views as $prepend_view){
$html .= $this->ci->load->view($prepend_view, $this->data, true);
}
}
}
$html .= $this->ci->load->view($view, $this->data, true);
if (!$no_append){
if (count($this->append_views)){
foreach ($this->append_views as $append_view){
$html .= $this->ci->load->view($append_view, $this->data, true);
}
}
}
if (!empty($this->callback)){
$html = call_user_func($this->callback, $html);
}
}
echo $html;
}
function get(){
return $this->data;
}
function prepend($view, $data = null){
if (!empty($view)){
$this->prepend_views[] = $view;
$this->set($data);
}
}
function set($data){
if (is_array($data)){
$this->data = array_merge($this->data, $data);
}
}
function set_callback($callback){
if (is_callable($callback)){
$this->callback = $callback;
}
}
}
/* End of file layout.php */
/* Location: ./application/libraries/layout.php */