Cheat Sheet速查表
跳转到主要内容

Handlebars.js 速查表

本文根据 Devhints / Handlebars.js 翻译。代码示例及版本范围沿用原文。

辅助方法 (Helpers)

Handlebars.registerHelper('link_to', function() {
  return "<a href='" + this.url + "'>" + this.body + "</a>";
})
var context = { posts: [{url: "/hello-world", body: "Hello World!"}] }
var source = "<ul>{{#posts}}<li>{{{link_to}}}</li>{{/posts}}</ul>"
var template = Handlebars.compile(source)
template(context)

渲染结果:

<ul>
  <li><a href='/hello-world'>Hello World!</a></li>
</ul>