Skip to content

Instantly share code, notes, and snippets.

@tylersticka
Created March 8, 2012 15:53
Show Gist options
  • Save tylersticka/2001635 to your computer and use it in GitHub Desktop.
Save tylersticka/2001635 to your computer and use it in GitHub Desktop.
Mustache layouts
<?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" => "&copy;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