How to have helpers in Hogan.js
Asked Answered
A

2

6

I am planning to use Hogan.js for my next project. I was trying to experiment with it a bit. I am just stuck and unable to find out how to use helpers with Hogan.js. I used to use to with Handlebars earlier. Is there a way to have a similar thing on Hogan?

Angi answered 13/12, 2013 at 12:50 Comment(1)
lambda's is all you get, for me thats enough. do you have a specific problem you cant solve with lambda's?Anastice
O
2

From hogan.js official website:

Hogan.js was developed against the mustache test suite, so everything that holds true for templates as specified here, is also the case for hogan.js.

Check out the mustache manpage for a thorough explanation of features. Especially the part on lambda expressions.

The following is an example comparison of implementation between hogan.js and handlebars.js.

Template

{{#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>'
    );
});

Output

<b>Willy is awesome.</b>
Outmoded answered 24/2, 2015 at 13:54 Comment(0)
R
1

I was having a hard time with this until I found this Hogan issue on Lambdas

It is not longer needed pass render to the helper.

Template

{{#foo}}
    Lets put this text in a html tag.
{{/foo}}

Hogan.js

"foo": function() {
    return function(text) {
        return "<p>" + text + "</p>"
    }

Output

<p>Lets put this text in a html tag.</p>

My problem was a little bit harder since I had:

Template

{{#foo}}
    {{bar}}
{{/foo}}

So the text being passed to the helper was just "{{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>"
    }
Roswell answered 26/2, 2016 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.