-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartial-loader.php
64 lines (56 loc) · 1.7 KB
/
partial-loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
$tmpl = <<<EOF
<div class="post">
{{> userMessage tagName="h1" }}
<h1>Comments</h1>
{{#each comments}}
{{> userMessage tagName="h2" }}
{{/each}}
</div>
EOF;
$data = array(
'author' => array('firstName' => 'Alan', 'lastName' => 'Johnson'),
'body' => 'I Love Handlebars',
'comments' => array(
array(
'author' => array('firstName' => 'Yehuda', 'lastName' => 'Katz'),
'body' => 'Me too!',
),
),
);
class PartialLoader implements IteratorAggregate, Handlebars\Registry {
private $impl;
private $partials = array();
public function __construct(Handlebars\Impl $impl) {
$this->impl = $impl;
}
public function offsetGet($offset) {
if( isset($partials[$offset]) ) {
return $partials[$offset];
}
// Transform partial name to file
$partialFile = $offset . '.hbs';
$impl = $this->impl;
// The partial's opcodes will be cached if opcode caching is enabled
// in the extension
return $partials[$offset] = function($context = null) use ($impl, $partialFile) {
return $impl->renderFile(__DIR__ . '/' . $partialFile, $context);
};
}
public function offsetSet($offset, $value) {
$this->partials[$offset] = $value;
}
public function offsetExists($offset) {
return null !== $this->offsetGet($offset);
}
public function offsetUnset($offset) {
unset($this->partials[$offset]);
}
public function getIterator() {
return new ArrayIterator($this->partials);
}
}
$vm = new Handlebars\VM();
$vm->setPartials(new PartialLoader($vm));
$output = $vm->render($tmpl, $data);
echo $output;