Adding new View_Layout class, to replace the layout controller completely.
This commit is contained in:
parent
0d4cec8f8c
commit
c60b276d26
|
@ -186,6 +186,13 @@ Controller:
|
|||
}
|
||||
} // End Welcome
|
||||
|
||||
Using the View_Layout class
|
||||
---
|
||||
|
||||
KOstache comes with a View_Layout class instead of a template controller. This allows your layouts to be more OOP and self contained, and they do not rely on your controllers so much.
|
||||
|
||||
To use it, have your view extend the View_Layout class. You can then specify your own layout file by placing it in templates/layout.mustache. At a minimum, it needs to have a {{>body}} partial defined in it.
|
||||
|
||||
For specific usage and documentation, see:
|
||||
|
||||
[PHP Mustache](http://github.com/bobthecow/mustache.php)
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
abstract class Controller_Layout extends Controller {
|
||||
|
||||
/**
|
||||
* @var string page layout
|
||||
*/
|
||||
public $template = 'layout';
|
||||
|
||||
/**
|
||||
* @var boolean auto render layout
|
||||
**/
|
||||
public $auto_render = TRUE;
|
||||
|
||||
/**
|
||||
* Loads the template [View] object.
|
||||
*/
|
||||
public function before()
|
||||
{
|
||||
if ($this->auto_render === TRUE)
|
||||
{
|
||||
// Load the layout
|
||||
$view_class = 'View_'.$this->template;
|
||||
$this->template = new $view_class;
|
||||
}
|
||||
|
||||
return parent::before();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns the layout [View] as the request response.
|
||||
*/
|
||||
public function after()
|
||||
{
|
||||
if ($this->auto_render === TRUE)
|
||||
{
|
||||
$this->request->response = $this->template;
|
||||
}
|
||||
|
||||
return parent::after();
|
||||
}
|
||||
|
||||
} // End Controller_Layout
|
|
@ -1,6 +1,8 @@
|
|||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class View_Layout extends Kostache {
|
||||
class View_Layout extends Kostache
|
||||
{
|
||||
const LAYOUT = 'layout';
|
||||
|
||||
/**
|
||||
* @var string template title
|
||||
|
@ -8,8 +10,23 @@ class View_Layout extends Kostache {
|
|||
public $title = 'Brought to you by KOstache!';
|
||||
|
||||
/**
|
||||
* @var string template content
|
||||
* Renders the body template into the layout
|
||||
*/
|
||||
public $content = 'Hello, world!';
|
||||
public function render($template = null, $view = null, $partials = null)
|
||||
{
|
||||
// Turn the child template into a partial
|
||||
$foo = explode('_', get_class($this));
|
||||
array_shift($foo);
|
||||
$view_location = strtolower(implode('/', $foo));
|
||||
|
||||
} // End View_Template
|
||||
$this->_partials+=array(
|
||||
'body' => file_get_contents(Kohana::find_file('templates', $view_location, 'mustache'))
|
||||
);
|
||||
|
||||
// Make the layout view the child class's template
|
||||
$this->_template = file_get_contents(Kohana::find_file('templates', self::LAYOUT, 'mustache'));
|
||||
|
||||
return parent::render($template, $view, $partials);
|
||||
}
|
||||
|
||||
} // End View_Layout
|
|
@ -6,6 +6,6 @@
|
|||
</head>
|
||||
<body>
|
||||
<h1>{{title}}</h1>
|
||||
<p>{{content}}</p>
|
||||
<p>{{>body}}</p>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue