Skip to content

Instantly share code, notes, and snippets.

@graphis
Forked from ernestas/gist:1033461
Created November 4, 2012 21:05
Show Gist options
  • Save graphis/4013780 to your computer and use it in GitHub Desktop.
Save graphis/4013780 to your computer and use it in GitHub Desktop.
KOstache layout in a layout
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Mustache templates for Kohana.
*
* @package Kostache
* @category Base
* @author Jeremy Bush <[email protected]>
* @author Woody Gilk <[email protected]>
* @copyright (c) 2010-2011 Jeremy Bush
* @copyright (c) 2011 Woody Gilk
* @license MIT
*/
abstract class Kohana_Kostache_Layout extends Kostache {
/**
* @var string partial name for content
*/
const CONTENT_PARTIAL = 'content';
/**
* @var boolean render template in layout?
*/
public $render_layout = TRUE;
/**
* @var string layout path
*/
protected static $_layout = 'layout';
/**
*
*/
public function render($child_layout = NULL)
{
if ( ! $this->render_layout)
{
return parent::render();
}
$partials = $this->_partials;
// is content partial a _template or child_layout
$partial = (NULL == $child_layout) ? $this->_template : $child_layout;
// add current template or child_layout as content partial
$partials[Kostache_Layout::CONTENT_PARTIAL] = $partial;
$template = $this->_load(static::$_layout);
// if this layout is called 'layout', meaning outermost layout,
// return output,
// else pass this layout as child_layout to parent_layout
if ('layout' == static::$_layout)
{
return $this->_stash($template, $this, $partials)->render();
}
else
{
$sub = $this->_stash($template, $this, $partials)->render();
$parent_layout_class_name = static::$_parent_layout;
$parent_layout_class = new $parent_layout_class_name();
return $parent_layout_class->render($sub);
}
}
}
<?php defined('SYSPATH') or die('No direct script access.');
class View_Layout extends KOstache_Layout
{
/**
* @var string layout path
*/
protected static $_layout = 'layout';
}
<?php defined('SYSPATH') or die('No direct script access.');
class View_Sub extends View_Layout
{
/**
* @var string layout path
*/
protected static $_layout = 'sub';
/**
* @var string layout will be set as partial in parent layout
*/
protected static $_parent_layout= 'View_Layout';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment