Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Created March 3, 2012 06:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehoshua02/1964782 to your computer and use it in GitHub Desktop.
Save jehoshua02/1964782 to your computer and use it in GitHub Desktop.
CodeIgniter page/layout/content views

Why I like this approach ...

I like this approach because it relieves the controllers from much of view rendering responsibility while still leaving the controller the power to alter the view rendering process a great deal.

Lingering concerns ...

However, my gut tells me that this approach still hides a non-trivial flaw. I wonder how this approach will cope with swapping out layouts or pages, and actually generating parts of the total view that require increased intricate logic and heavier data requirements.

The goal ...

Whether this approach, a refinement of this approach, or another yet to be discovered, the goal is to maintain the singularity of each controller, adhere to the DRY principle, and provide flexibility in rendering views.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends P_Controller {
public function index()
{
// TODO Product->index()
}
public function add()
{
// render form
$this->data = array(
'action' => base_url('product/add'),
'product' => $this->input->post('product')
);
}
}
/* End of file product.php */
/* Location: ./application/controllers/product.php */
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class P_Controller extends CI_Controller {
protected $content_view = NULL;
protected $data = array();
protected $layout_view = 'layout/default';
protected $layout_data = array();
protected $page_view = 'page/html5';
protected $page_data = array();
public function _output($output)
{
// render content
if ($this->content_view === FALSE) return;
/**
* Unless there is a very good reason, suppressing errors is a bad idea.
* In this case, notice errors are suppressed for two good reasons:
*
* 1. To prevent redundant variables checks in the templates.
* 2. To keep this class ignorant of what variables templates expect.
*
* The original error level is restored after templates are rendered.
*/
$error_level = error_reporting(~E_NOTICE);
if (empty($this->content_view))
{
$this->content_view = $this->router->class . '/' . $this->router->method;
}
if ($this->content_view == 'json')
{
$output = json_encode($this->data);
}
else
{
$output = $this->load->view($this->content_view, $this->data, TRUE);
// render layout
if ( ! empty($this->layout_view))
{
$this->layout_data['content'] = $output;
$output = $this->load->view($this->layout_view, $this->layout_data, TRUE);
}
// render page
if ( ! empty($this->page_view))
{
$this->page_data['content'] = $output;
$output = $this->load->view($this->page_view, $this->page_data, TRUE);
}
}
echo $output;
error_reporting($error_level);
}
}
/* End of file P_Controller.php */
/* Location: ./application/core/P_Controller.php */
<?php
/**
* View for default layout of <body> inner html.
* @var string $header string The visible header of the page.
* @var string $content string The main content of the page.
* @var string $footer string The footer of the page.
*/
?>
<div id="container">
<?php echo $header ?>
<?php echo $content ?>
<?php echo $footer ?>
</div><!-- end container -->
<?php
/**
* View for basic HTML5 document structure.
* @var string $meta string Meta tags. Used for things like keywords and descriptions.
* @var string $title string Title of the document.
* @var string $css string Internal or external html CSS style elements (<link> or <style>).
* @var string $javascript string Internal or external javascript (<script>).
* @var string $content string Content representing the inner html for the <body> element.
*/
?>
<!DOCTYPE html>
<html>
<head>
<!-- meta -->
<meta charset="utf-8" />
<?php echo $meta; ?>
<!-- title -->
<title><?php echo $title; ?></title>
<!-- css -->
<?php echo $css; ?>
<!-- javascript -->
<?php echo $javascript; ?>
</head>
<body>
<?php echo $content ?>
</body>
</html>
<?php
/**
* View for product add functionality
* @var $action string Action for the form.
* @var $product array Array of product data.
*/
?>
<div id="product_add">
<h2>Add Product</h2>
<form action="<?php echo $action ?>" method="post">
<label>SKU <input type="text" name="product[sku]" value="<?php echo $product['sku'] ?>" /></label>
<label>Name <input type="text" name="product[name]" value="<?php echo $product['name'] ?>" /></label>
<input type="submit" name="product[submit]" value="Save" />
</form>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment