OSDN Git Service

Regular updates
[twpd/master.git] / handlebars.js.md
1 ---
2 title: Handlebars.js
3 category: JavaScript libraries
4 layout: 2017/sheet
5 weight: -1
6 ---
7
8 {% raw %}
9
10 ### Helpers
11
12 ```js
13 Handlebars.registerHelper('link_to', function() {
14   return "<a href='" + this.url + "'>" + this.body + "</a>";
15 })
16 ```
17
18 ```js
19 var context = { posts: [{url: "/hello-world", body: "Hello World!"}] }
20 var source = "<ul>{{#posts}}<li>{{{link_to}}}</li>{{/posts}}</ul>"
21 ```
22
23 ```js
24 var template = Handlebars.compile(source)
25 template(context)
26 ```
27
28 Would render:
29
30 ```html
31 <ul>
32   <li><a href='/hello-world'>Hello World!</a></li>
33 </ul>
34 ```
35
36 {% endraw %}