我计划在我的下一个项目中使用Hogan.js。我试图进行一些实验。但是,我遇到了困难,无法找出如何在Hogan.js中使用helpers。我之前使用Handlebars时经常使用它。是否有一种类似的方法可以在Hogan上使用?
我计划在我的下一个项目中使用Hogan.js。我试图进行一些实验。但是,我遇到了困难,无法找出如何在Hogan.js中使用helpers。我之前使用Handlebars时经常使用它。是否有一种类似的方法可以在Hogan上使用?
来自hogan.js官网:
Hogan.js是针对Mustache测试套件开发的,因此在这里规定的模板相关的所有内容也适用于Hogan.js。
详细了解特性,请查看Mustache手册。尤其是关于Lambda表达式的部分。
下面是Hogan.js和Handlebars.js实现之间的示例比较。
模板
{{#bold}}
Willy is awesome.
{{/bold}}
Hogan.js
{
"bold": function() {
return function(text, render) {
return "<b>" + render(text) + "</b>"
}
}
}
Handlebars.js
Handlebars.registerHelper('bold', function(options) {
return new Handlebars.SafeString(
'<b>' + options.fn(this) + '</b>'
);
});
输出
<b>Willy is awesome.</b>
{{#foo}}
Lets put this text in a html tag.
{{/foo}}
Hogan.js
"foo": function() {
return function(text) {
return "<p>" + text + "</p>"
}
输出
<p>Lets put this text in a html tag.</p>
{{#foo}}
{{bar}}
{{/foo}}
因此,传递给助手的text
只是"{{bar}}"
Hogan.js
"foo": function() {
return function(text) {
// First get the rendered bar variable
var bar = Hogan.compile(text).render(this));
return "<p>" + bar + "</p>"
}