如何在Hogan.js中找到帮手

时间:2013-12-13 12:50:03

标签: javascript handlebars.js mustache hogan.js

我计划在下一个项目中使用Hogan.js。我试着尝试一下。我只是陷入困境,无法找到如何使用Hogan.js的助手。我以前习惯使用Handlebars。有没有办法在Hogan上有类似的东西?

2 个答案:

答案 0 :(得分:3)

来自hogan.js official website

  

Hogan.js是针对小胡子测试套件开发的,因此对于hogan.js来说,这里指定的模板的所有内容都是如此。

查看mustache manpage以获取有关功能的详尽说明。特别是关于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 :(得分:2)

我遇到了这段困难,直到找到this Hogan issue on Lambdas

不再需要将渲染传递给帮助者。

<强>模板

{{#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>"
    }