Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Created March 2, 2012 22:30
Show Gist options
  • Save jehoshua02/1961900 to your computer and use it in GitHub Desktop.
Save jehoshua02/1961900 to your computer and use it in GitHub Desktop.
CI layout demonstrated by Shawn McCool heybigname.com/2011/08/26/codeigniter-2-sparks-php-activerecord-part-4-views-and-layouts/
<?php
class Auth extends MY_Controller
{
function login()
{
if ($_POST)
{
// The user has submitted the login form
$user = User::validate_login($_POST['username'], $_POST['password']);
if ($user)
redirect('dashboard');
else
$this->view_data['message'] = 'Invalid user/password.';
}
}
}
<?php
class MY_Controller extends CI_Controller {
protected $layout_view = 'application';
protected $content_view = '';
protected $view_data = array();
public function _output($output)
{
if ($this->content_view !== FALSE && empty($this->content_view)) ? $this->content_view = $this->router->class . '/' . $this->router->method;
$yield = file_exists(APPPATH . 'views/' . $this->content_view . EXT) ? $this->load->view($this->content_view, $this->view_data, TRUE) : FALSE;
if ($this->layout_view)
echo $this->load->view('layouts/' . $this->layout_view, array('yield' => $yield), TRUE);
else
echo $yield;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment