Skip to content Skip to sidebar Skip to footer

How To Get The Value When Using Lambda In Hogan.js

I have the following function which handles AJAX success callback in jQuery: function success(data) { var templateData = { items: data, formatMoney: function ()

Solution 1:

Yep.

Mustache implementations that scrictly adhere to the spec are very limiting. Your option would be, in your lambda code, to render the "{{Cost}}" string (you should get this string and a rendering function as your lambda parameters), and parse the rendered string into a float, your cost.

Not clean, for sure. But it would work. Don't forget to open an issue in the repository of the Mustache implementation you are using.

Solution 2:

I think we have two options.

1) Use lambdas in hogan.js

res.render("template", {
          lambdas:{
            formatMoney: function( cost ){
              return cost.format('usd');
            }
          });

and the template should be

{#Items}}
<tr>
    <td>{{ProductId}}</td>
    <td>{{#lambdas.formatMoney}}{{Cost}}{{/lambdas.formatMoney}}</td>
</tr>
{{/Items}}

2) As stated in the question, we can use this object.

Javascript code is

res.render("template", {
  formatMoney:{
    return function(key){
      var cost = this[key];
      return cost.format('usd');
    };
  }
});

and the template is

{#Items}}
<tr>
    <td>{{ProductId}}</td>
    <td>{{#formatMoney}}Cost{{/formatMoney}}</td>
</tr>
{{/Items}}

Post a Comment for "How To Get The Value When Using Lambda In Hogan.js"