如何在Hogan.js中使用helpers

6

我计划在我的下一个项目中使用Hogan.js。我试图进行一些实验。但是,我遇到了困难,无法找出如何在Hogan.js中使用helpers。我之前使用Handlebars时经常使用它。是否有一种类似的方法可以在Hogan上使用?


lambda表达式就是你所需要的,对我来说已经足够了。你有什么特定的问题无法用lambda表达式解决吗? - Paul Scheltema
2个回答

2

来自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>

1
我在处理这个问题时遇到了困难,直到我找到了关于Lambdas的Hogan问题
现在不再需要将渲染传递给helper。 模板
{{#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>"
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接