【问题标题】:How to have helpers in Hogan.js如何在 Hogan.js 中使用助手
【发布时间】:2013-12-13 12:50:02
【问题描述】:

我计划在我的下一个项目中使用 Hogan.js。我试着用它做一些实验。我只是卡住了,无法找到如何在 Hogan.js 中使用助手。我以前习惯使用 Handlebars。有没有办法在 Hogan 上有类似的东西?

【问题讨论】:

  • lambda 是你所得到的,对我来说就足够了。您有无法使用 lambda 解决的特定问题吗?

标签: javascript handlebars.js mustache hogan.js


【解决方案1】:

来自hogan.js official website

Hogan.js 是针对 mustache 测试套件开发的,因此适用于此处指定的模板的所有内容也适用于 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>

【讨论】:

    【解决方案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>"
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      相关资源
      最近更新 更多