Created
March 8, 2012 15:53
-
-
Save tylersticka/2001635 to your computer and use it in GitHub Desktop.
Mustache layouts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'lib/Mustache/Mustache.php'; | |
class view extends mustache { | |
public $sections = array(); | |
public function __construct($layout, $page, $view) { | |
$this->renderPage($page); | |
parent::__construct($this->render($layout, $this->sections), $view); | |
} | |
public function renderPage($template) { | |
list($before, $type, $tag, $content, $after) = $this->_findSection($template); | |
$this->sections[$tag] = $content; | |
if ($after) $this->renderPage($after); | |
} | |
} | |
$layout = <<< BLOCK | |
<header> | |
{{& header}} | |
</header> | |
<section> | |
{{& body}} | |
</section> | |
<footer> | |
{{& footer}} | |
</footer> | |
BLOCK; | |
$page = <<< BLOCK | |
{{#header}} | |
<h1>{{title}}</h1> | |
{{/header}} | |
{{#body}} | |
<p>{{content}}</p> | |
{{/body}} | |
{{#footer}} | |
<span>{{{copyright}}}</span> | |
{{/footer}} | |
BLOCK; | |
?> | |
<!doctype html> | |
<html> | |
<body> | |
<?php | |
$data = array( | |
"title" => "Title of the page", | |
"content" => "This is some content", | |
"copyright" => "©2012 Foo" | |
); | |
$m = new view($layout, $page, $data); | |
echo $m->render(); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment