Skip to content

Commit 93ef15a

Browse files
committed
Add examples
1 parent b60be29 commit 93ef15a

13 files changed

+352
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ echo $vm->render('{{foo}}', array('foo' => 'bar'));
5151
echo $vm->renderFile('/path/to/foo.hbs', array('foo' => 'bar'));
5252
```
5353

54+
See the [examples](examples) folder for more examples.
55+
5456

5557
## License
5658

examples/block-expressions.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
// From http://handlebarsjs.com/ - Block Expressions
4+
5+
$tmpl = <<<EOF
6+
{{#list people}}{{firstName}} {{lastName}}{{/list}}
7+
EOF;
8+
$data = array(
9+
'people' => array(
10+
array('firstName' => 'Yehuda', 'lastName' => 'Katz'),
11+
array('firstName' => 'Carl', 'lastName' => 'Lerche'),
12+
array('firstName' => 'Alan', 'lastName' => 'Johnson'),
13+
)
14+
);
15+
16+
$helpers = new Handlebars\DefaultRegistry();
17+
$helpers['list'] = function($items, Handlebars\Options $options) {
18+
$out = '<ul>';
19+
foreach( $items as $item ) {
20+
$out .= '<li>' . $options->fn($item) . '</li>';
21+
}
22+
return $out . '</ul>';
23+
};
24+
25+
$vm = new Handlebars\VM();
26+
$vm->setHelpers($helpers);
27+
echo $vm->render($tmpl, $data);

examples/comments.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
$tmpl = <<<EOF
4+
<div class="entry">
5+
{{! This comment will not be in the output }}
6+
<!-- This comment will be in the output -->
7+
</div>
8+
EOF;
9+
10+
$vm = new Handlebars\VM();
11+
echo $vm->render($tmpl);

examples/getting-started.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
// From http://handlebarsjs.com/ - Getting Started
4+
5+
$tmpl = <<<EOF
6+
<div class="entry">
7+
<h1>{{title}}</h1>
8+
<div class="body">
9+
{{body}}
10+
</div>
11+
</div>
12+
EOF;
13+
14+
$data = array(
15+
'title' => 'My New Post',
16+
'body' => 'This is my first post!',
17+
);
18+
19+
$vm = new Handlebars\VM();
20+
echo $vm->render($tmpl, $data);

examples/helper-loader.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
$tmpl = <<<EOF
4+
<div class="post">
5+
<h1>By {{fullName author}}</h1>
6+
<div class="body">{{body}}</div>
7+
8+
<h1>Comments</h1>
9+
10+
{{#each comments}}
11+
<h2>By {{fullName author}}</h2>
12+
<div class="body">{{body}}</div>
13+
{{/each}}
14+
</div>
15+
EOF;
16+
17+
$data = array(
18+
'author' => array('firstName' => 'Alan', 'lastName' => 'Johnson'),
19+
'body' => 'I Love Handlebars',
20+
'comments' => array(
21+
array(
22+
'author' => array('firstName' => 'Yehuda', 'lastName' => 'Katz'),
23+
'body' => 'Me too!',
24+
),
25+
),
26+
);
27+
28+
class FullNameHelper {
29+
public function __invoke($person) {
30+
return $person['firstName'] . ' ' . $person['lastName'];
31+
}
32+
}
33+
34+
class HelperLoader implements IteratorAggregate, Handlebars\Registry {
35+
private $helpers = array();
36+
public function offsetGet($offset) {
37+
if( isset($helpers[$offset]) ) {
38+
return $helpers[$offset];
39+
}
40+
$className = ucfirst($offset) . 'Helper'; // Transform helper name to class
41+
if( class_exists($className, true) ) {
42+
return $helpers[$offset] = new $className;
43+
}
44+
}
45+
public function offsetSet($offset, $value) {
46+
$this->helpers[$offset] = $value;
47+
}
48+
public function offsetExists($offset) {
49+
return null !== $this->offsetGet($offset);
50+
}
51+
public function offsetUnset($offset) {
52+
unset($this->helpers[$offset]);
53+
}
54+
public function getIterator() {
55+
return new ArrayIterator($this->helpers);
56+
}
57+
}
58+
59+
$vm = new Handlebars\VM();
60+
$vm->setHelpers(new HelperLoader());
61+
echo $vm->render($tmpl, $data);

examples/helpers-2.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
$tmpl = <<<EOF
4+
<ul>
5+
{{#each items}}
6+
<li>{{agree_button}}</li>
7+
{{/each}}
8+
</ul>
9+
EOF;
10+
11+
$data = array(
12+
'items' => array(
13+
array('name' => 'Handlebars', 'emotion' => 'love'),
14+
array('name' => 'Mustache', 'emotion' => 'enjoy'),
15+
array('name' => 'Ember', 'emotion' => 'want to learn'),
16+
),
17+
);
18+
19+
$helpers = new Handlebars\DefaultRegistry();
20+
$helpers['agree_button'] = function(Handlebars\Options $options) {
21+
$emotion = htmlspecialchars($options->scope['emotion']);
22+
$name = htmlspecialchars($options->scope['name']);
23+
$result = "<button>I agree. I " . $emotion . " " . $name . "</button>";
24+
return new Handlebars\SafeString($result);
25+
};
26+
27+
$vm = new Handlebars\VM();
28+
$vm->setHelpers($helpers);
29+
echo $vm->render($tmpl, $data);

examples/helpers.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
$tmpl = <<<EOF
4+
<div class="post">
5+
<h1>By {{fullName author}}</h1>
6+
<div class="body">{{body}}</div>
7+
8+
<h1>Comments</h1>
9+
10+
{{#each comments}}
11+
<h2>By {{fullName author}}</h2>
12+
<div class="body">{{body}}</div>
13+
{{/each}}
14+
</div>
15+
EOF;
16+
17+
$data = array(
18+
'author' => array('firstName' => 'Alan', 'lastName' => 'Johnson'),
19+
'body' => 'I Love Handlebars',
20+
'comments' => array(
21+
array(
22+
'author' => array('firstName' => 'Yehuda', 'lastName' => 'Katz'),
23+
'body' => 'Me too!',
24+
),
25+
),
26+
);
27+
28+
$helpers = new Handlebars\DefaultRegistry();
29+
$helpers['fullName'] = function($person) {
30+
return $person['firstName'] . ' ' . $person['lastName'];
31+
};
32+
33+
$vm = new Handlebars\VM();
34+
$vm->setHelpers($helpers);
35+
echo $vm->render($tmpl, $data);

examples/html-escaping.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
// From http://handlebarsjs.com/ - HTML Escaping
4+
5+
$tmpl = <<<EOF
6+
<div class="entry">
7+
<h1>{{title}}</h1>
8+
<div class="body">
9+
{{{body}}}
10+
</div>
11+
</div>
12+
EOF;
13+
14+
$vm = new Handlebars\VM();
15+
echo $vm->render($tmpl, array(
16+
'title' => "All about <p> Tags",
17+
'body' => '<p>This is a post about &lt;p&gt; tags</p>',
18+
));

examples/partial-loader.php

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

examples/partials.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
$tmpl = <<<EOF
4+
<div class="post">
5+
{{> userMessage tagName="h1" }}
6+
7+
<h1>Comments</h1>
8+
9+
{{#each comments}}
10+
{{> userMessage tagName="h2" }}
11+
{{/each}}
12+
13+
</div>
14+
EOF;
15+
16+
$data = array(
17+
'author' => array('firstName' => 'Alan', 'lastName' => 'Johnson'),
18+
'body' => 'I Love Handlebars',
19+
'comments' => array(
20+
array(
21+
'author' => array('firstName' => 'Yehuda', 'lastName' => 'Katz'),
22+
'body' => 'Me too!',
23+
),
24+
),
25+
);
26+
27+
$partials = new Handlebars\DefaultRegistry();
28+
$partials['userMessage'] = '<{{tagName}}>By {{author.firstName}} {{author.lastName}}</{{tagName}}>' . "\n"
29+
. '<div class="body">{{body}}</div>';
30+
31+
$vm = new Handlebars\VM();
32+
$vm->setPartials($partials);
33+
echo $vm->render($tmpl, $data);

examples/paths.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
$tmpl = <<<EOF
4+
<h1>Comments</h1>
5+
6+
<div id="comments">
7+
{{#each comments}}
8+
<h2><a href="/posts/{{../permalink}}#{{id}}">{{title}}</a></h2>
9+
<div>{{body}}</div>
10+
{{/each}}
11+
</div>
12+
EOF;
13+
14+
$data = array(
15+
'permalink' => '2014-08-08',
16+
'comments' => array(
17+
array(
18+
'id' => '1892',
19+
'title' => 'My title',
20+
'body' => 'Body',
21+
)
22+
)
23+
);
24+
25+
$vm = new Handlebars\VM();
26+
echo $vm->render($tmpl, $data);

examples/safe-string.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
// From http://handlebarsjs.com/ - HTML Escaping
4+
5+
$tmpl = <<<EOF
6+
<div>
7+
{{link text url}}
8+
</div>
9+
EOF;
10+
11+
$helpers = new Handlebars\DefaultRegistry();
12+
$helpers['link'] = function($text, $url) {
13+
$text = htmlspecialchars($text);
14+
$url = htmlspecialchars($url);
15+
$result = '<a href="' . $url . '">' . $text . '</a>';
16+
return new Handlebars\SafeString($result);
17+
};
18+
19+
$vm = new Handlebars\VM();
20+
$vm->setHelpers($helpers);
21+
echo $vm->render($tmpl, array(
22+
'url' => "https://google.com",
23+
'text' => "Google",
24+
));

examples/userMessage.hbs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<{{tagName}}>By {{author.firstName}} {{author.lastName}}</{{tagName}}>
2+
<div class="body">{{body}}</div>

0 commit comments

Comments
 (0)