Skip to content

Instantly share code, notes, and snippets.

@alxhill
Created February 24, 2011 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alxhill/843021 to your computer and use it in GitHub Desktop.
Save alxhill/843021 to your computer and use it in GitHub Desktop.
A simple view controller for CodeIgniter/my gallery system.

First load the library $this->load->library('view');

Then add messages, data, templates etc $this->view->template('admin')->title('Admin control panel')->message->('success','You have successfully logged in')->data($data);

Finally, load the view. Done! $this->view->load();

<?php
/*
View controller for showing views. Adds spedific data, and allows selection of a custom title, template, message and data.
*/
class View {
private $template = null;
private $title = null;
private $message = null;
private $data = null;
public function template($template)
{
$this->template = $template;
return $this;
}
public function title($title)
{
$this->title = $title;
return $this;
}
public function message($class,$message)
{
$this->message = array('class' => $class, 'message' => $message);
return $this;
}
public function data($data)
{
if(is_array($data))
{
$this->data = $data;
return $this;
}
else
{
return FALSE;
}
}
public function load()
{
$CI =& get_instance();
$CI->load->library('session');
if (!(isset($this->template)))
{
return FALSE;
}
else
{
$view_data = null;
if (isset($this->data))
{
foreach ($this->data as $key=>$value)
{
$view_data[$key] = $value;
}
}
$header_data['title'] = (isset($this->title)) ? 'Kutt Out Studios // ' . $this->title : 'Kutt Out Studios';
if (isset($this->message['class']) && isset($this->message['message']))
{
$header_data['class'] = $this->message['class'];
$header_data['message'] = $this->message['message'];
}
$nav_data['galleries'] = $this->get_galleries();
$header_data['logged_in'] = $CI->session->userdata('logged_in');
$view['header'] = $CI->load->view('gallery/common/header', $header_data, TRUE);
$view['nav'] = $CI->load->view('gallery/common/nav', $nav_data, TRUE);
$view['main'] = $CI->load->view('gallery/' . $this->template, $view_data, TRUE);
$view['footer'] = $CI->load->view('gallery/common/footer','', TRUE);
$CI->load->view('view', $view);
$this->reset();
return TRUE;
}
}
public function reset()
{
$this->title = null;
$this->message = null;
$this->template = null;
$this->data = null;
}
private function get_galleries()
{
$CI =& get_instance();
$CI->load->model('gallery_model');
$galleries = $CI->gallery_model->get_all_galleries();
foreach ($galleries as $gallery)
{
$all_galleries[] = $gallery['name'];
}
return $all_galleries;
}
}
<?php
//This is the view loaded at the end, only used such that CodeIgniter can set the headers as necessary.
echo $header.$nav.$main.$footer;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment