How to get helpers in Hogan.js

I plan to use Hogan.js for my next project. I tried to experiment with him a little. I was just stuck and couldn't find out how to use helpers with Hogan.js. I used to use Handlebars. Is there a way to do something similar to Hogan?

+6
source share
2 answers

From hogan.js official website :

Hogan.js was designed against a mustache test suite, so everything that is true for templates, as mentioned here, also applies to hogan.js.

Check out the mustache page for a detailed explanation of the features. This is especially true for lambda expressions.

The following is an example of comparing the 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> 
+3
source

I had a hard time with this until I found this Hogan problem on Lambdas

You no longer need to hand over the render to the assistant.

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 has been a bit more complicated ever since I:

Template

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

So, text is passed to the helper only "{{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>" } 
+2
source

All Articles