Skip to content

Instantly share code, notes, and snippets.

@graphis
Forked from wondersloth/base.php
Created November 4, 2012 20:50
Show Gist options
  • Save graphis/4013721 to your computer and use it in GitHub Desktop.
Save graphis/4013721 to your computer and use it in GitHub Desktop.
Kohana Page Rendering Scheme
<?php defined('SYSPATH') or die('No direct script access.');
abstract class Controller_SiteLayout_Base extends Controller {
public $template = 'layout/base/page';
public $auto_render = TRUE;
/**
* The before() method is called before your controller action.
* In our template controller we override this method so that we can
* set up default values. These variables are then available to our
* controllers if they need to be modified.
*
* @return the result from <code>parent::before()</code> if anything.
* @author Matt
*/
public function before()
{
$config = array(
'zones' => array(
'page_head' => 'layout/base/zone/page_head',
'page_top' => 'layout/base/zone/page_top',
'header' => 'layout/base/zone/header',
'content' => 'layout/base/zone/empty',
'footer' => 'layout/base/zone/footer',
'page_bottom' => 'layout/base/zone/page_bottom',
)
);
$view = View_HTML::factory($this->template, array(), $config);
$this->template = $view;
if ($this->auto_render) {
// Nothing
}
return parent::before();
}
/**
* The after() method is called after your controller action.
* In our template controller we override this method so that we can
* make any last minute modifications to the template before anything
* is rendered.
*
* @return void
* @author Matt
*/
public function after()
{
if ($this->auto_render) {
$this->request->response = $this->template->render();
}
parent::after();
}
}
<?php
class View_Grid extends View
{
public function __construct($file = NULL, array $data = NULL, $config = array())
{
parent::__construct($file, $data);
$this->zones = array();
if (!empty($config) && isset($config['zones']) && !empty($config['zones'])) {
foreach ($config['zones'] as $name => $path) {
$this->zones[$name] = self::factory($path);
}
}
}
public static function factory($file = NULL, array $data = NULL, $config = array())
{
return new self($file, $data, $config);
}
public function set_zone($zone, $view)
{
$this->zones[$zone] = !($view instanceof View) ? self::factory($view) : $view;
return $this;
}
public function set_zone_content($zone, $key, $value)
{
$view = $this->get_zone($zone);
$view->set($key, $value);
return $this;
}
public function get_zone($zone)
{
if (!isset($this->zones[$zone])) {
$this->zones[$zone] = new self;
}
return $this->zones[$zone];
}
}
<?php
class View_HTML extends View_Grid
{
public function __construct($file = NULL, array $data = NULL, $config = array())
{
parent::__construct($file, $data, $config);
$this->title = (!empty($data['title'])) ? $data['title'] : 'No Title';
$this->get_zone('page_head')->css = (!empty($data['css'])) ? $data['css'] : array();
$this->get_zone('page_head')->css_ie = (!empty($data['css_ie'])) ? $data['css_ie'] : array();
$this->get_zone('page_head')->js = (!empty($data['js'])) ? $data['js'] : array();
$this->get_zone('page_bottom')->js_deferred = (!empty($data['js_deferred'])) ? $data['js_deferred'] : array();
}
public static function factory($file = NULL, array $data = NULL, $config = array())
{
return new self($file, $data, $config);
}
public function add_css($path, $media = 'screen')
{
$this->get_zone('page_head')->css[] = array($path, $media);
return $this;
}
public function add_css_ie($version, $path, $media = 'screen')
{
$this->get_zone('page_head')->css_ie[] = array($version, $path, $media);
return $this;
}
public function add_js($path)
{
$this->get_zone('page_head')->js[] = $path;
return $this;
}
public function add_js_deferred($path)
{
$this->get_zone('page_bottom')->js_deferred[] = $path;
return $this;
}
public function set_title($title)
{
$this->title = $title;
return $this;
}
}
<?php
extract($zones, EXTR_OVERWRITE);
// If no filename is set for the zone, then we define a default one.
if (!($page_head->isset_filename())) {
$page_head->set_filename('layout/base/zone/page_head');
}
if (!$page_top->isset_filename()) {
$page_top->set_filename('layout/base/zone/page_top');
}
if (!$header->isset_filename()) {
$header->set_filename('layout/base/zone/header');
}
if (!$content->isset_filename()) {
$content->set_filename('layout/base/zone/blank');
}
if (!$footer->isset_filename()) {
$footer->set_filename('layout/base/zone/footer');
}
if (!$page_bottom->isset_filename()) {
$page_bottom->set_filename('layout/base/zone/page_bottom');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php echo $title; ?></title>
<?php echo $page_head->render(); ?>
</head>
<body>
<!-- BEGIN page-top -->
<?php echo $page_top->render(); ?>
<!-- END page-top -->
<div id="main-content-container" class="page-container">
<div class="page-header">
<?php echo $header->render(); ?>
</div>
<div class="page-content">
<?php echo $content->render(); ?>
</div>
<div class="page-footer">
<?php echo $footer->render(); ?>
</div>
</div>
<!-- BEGIN page-bottom -->
<?php echo $page_bottom->render(); ?>
<!-- END page-bottom -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment