Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 11 revisions

Category:Library::Template Engines | Category:Library::Community [h3]Introduction[/h3] On the forums where many questions about embedded views. There is an article written about this topic. But if you just have started using code ingniter it is sometimes easier to rip. Than to learn.

When I started using CI I thought it would be easy to have only one view. But I never realised how many times you include a page into an other one. So it was time to create an solution which will be help me out. Stop including and use the CI way!

[h3]Features[/h3] It is not very difficult what this class will do for you but it saves some time when you always want to create multi-view pages. The most important things what this lib will do for you are:

  • Get default views from the config file and put them together.
  • Make it possible to use more themes.

[h3]Usage[/h3] It will be very easy to use this class. I will give you some examples to explain it.

first we have to set some defaults in the config file. [code]//template config $config['theme'] = "Default/"; $config['masterTemplate'] = 'template/master.php'; $config['pageHeader'] = 'template/header.php'; $config['pageFooter'] = 'template/footer.php'; $config['leftCol'] = 'template/left_col.php'; $config['rightCol'] = 'template/right_col.php';

$config['adminMasterTemplate'] = 'master.php'; $config['adminPageHeader'] = 'header.php'; $config['adminPageFooter'] = 'footer.php'; $config['adminLeftCol'] = 'left_col.php'; $config['adminRightCol'] = 'right_col.php'; [/code]

the admin templates are placed in the folder:

application_path/views/$config['theme']/template_admin

All other templates need to be placed in the $config['theme'] folder in the views folder. now we have done this we can create or first controller:

[code]class About extends Controller {

function About() { parent::Controller(); }

function index() { $data['title'] = "About the Code Igniter Sample Application"; $this->load->view('content/index', $data); } }[/code] this like you should do it the normal way. using Viewlib will be like this

[code]class About extends Controller {

function About() { parent::Controller(); }

function index() { $this->load->library('viewlib'); $data['title'] = "About the Code Igniter Sample Application"; $this->viewlib->setdata($data);
$this->viewlib->view('content/forum/reply'); } }[/code]

Notice we have replaced the load->view for ViewLib. From now our content place in the folder
application_path/views/$config['theme']/content will be shown in our theme. That's all fox.

Ooh! I have forgotten something important. THE CODE of the LIB.

[h3]Source[/h3] [code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Viewlib{ var $right_data; var $footer_data; var $data; var $header; var $footer; var $left; var $right; var $theme;

function Viewlib() { $obj =& get_instance(); $this->header = $obj->config->item('pageHeader'); $this->footer = $obj->config->item('pageFooter'); $this->left = $obj->config->item('leftCol'); $this->right = $obj->config->item('rightCol'); $this->theme = $obj->config->item('theme');

}

function setData($data) { $this->data = $data; }

function setRight($data) { $this->right_data = $data; }

function setFooter($data) { $this->footer_data = $data; } function view($content, $admins = false, $templates = '') { $obj =& get_instance(); $template = ($admins === true)?$obj->config->item('theme').'template_admin/':$obj->config->item('theme'); if($admins === true){ $this->header = $obj->config->item('adminPageHeader'); $this->footer = $obj->config->item('adminPageFooter'); $this->left = $obj->config->item('adminLeftCol'); $this->right = $obj->config->item('adminRightCol'); $templates['master'] = $obj->config->item('adminMasterTemplate'); }

$page['pageHeader'] = '';
$page['leftCol'] = ''; 
$page['rightCol'] = '';
$page['pageFooter'] = '';
if(!isset($templates['header']) || $templates['header'] != false )
{   
 if(isset($templates['header']))
 $this->header =  $templates['header'];
 
 $page['pageHeader'] =  $obj->load->view($template.$this->header, $this->data, true);
}
if(!isset($templates['left']) || $templates['left'] != false)
{
 if(isset($templates['left']))
 $this->left =  $templates['left'];
 
 $page['leftCol'] =  $obj->load->view($template.$this->left, '', true);
}

if(!isset($templates['right']) || $templates['right'] != false)
{
 if(isset($templates['right']))
 $this->right =  $templates['right'];
 
 $page['rightCol'] =  $obj->load->view($template.$this->right, $this->right_data, true);
}
if(!isset($templates['footer']) || $templates['footer'] != false)
{
if(isset($templates['footer']))
$this->footer =  $templates['footer'];
$page['pageFooter'] =  $obj->load->view($template.$this->footer, $this->footer_data, true);
}
$page['content'] =  $obj->load->view($template.$content, $this->data, true);
if(isset($templates['master']) && $templates['master'] != false)
$obj->load->view($template.$templates['master'], $page);
else
$obj->load->view($template.$obj->config->item('masterTemplate'), $page);

}

function preview($content, $data){ $obj =& get_instance(); return $obj->load->view($this->theme.$content, $data, true); }
function alert($message) { echo "alert ('{$message}')</script>"; }

function alert_refresh($message, $refresh) { echo "alert ('{$message}')</script>"; echo '<meta http-equiv="refresh" content="0;url='.site_url($refresh).'" />'; } } ?>[/code]

Clone this wiki locally