hogan.js icon indicating copy to clipboard operation
hogan.js copied to clipboard

render is undefined

Open rouberg opened this issue 9 years ago • 5 comments

code fragment below is copy from mustache , but I can't get argument render in Lambdas. The console told me argument render is undefined. Is there something that i misunderstand?

Template:

{{#wrapped}} {{name}} is awesome. {{/wrapped}}

Hash:

{ "name": "Willy", "wrapped": function() { return function(text, render) { //actually param render cannot get a argument return "" + render(text) + "" } } }

rouberg avatar May 20 '15 09:05 rouberg

@Rouberg Lambdas in Hogan.js is broken. Please try that.

{
  "wrapped": function() {
    return function(text) {
      return Hogan.compile(text).render(this);
    }
  }
}

ex1st avatar May 27 '15 22:05 ex1st

See #199

Jerska avatar Jun 18 '15 01:06 Jerska

The 'render' argument isn't in the mustache spec.

sayrer avatar Jun 22 '15 04:06 sayrer

@sayrer

Lambdas

When the value is a callable object, such as a function or lambda, the object will be invoked and passed the block of text. The text passed is the literal block, unrendered. {{tags}} will not have been expanded - the lambda should do that on its own. In this way you can implement filters or caching.

Template:

{{#wrapped}}
  {{name}} is awesome.
{{/wrapped}}

Hash:

{
  "name": "Willy",
  "wrapped": function() {
    return function(text, render) {
      return "<b>" + render(text) + "</b>"
    }
  }
}

Output:

<b>Willy is awesome.</b>

You're right, the text only states that the lambda should receive the unrendered chunk of text. But by looking at the examples, it seems pretty obvious that this is how one could expect hogan.js to behave.
And it would allow to have the same hogan options that Hogan.compile was invoked with instead of having to store them somewhere.

Jerska avatar Jun 22 '15 04:06 Jerska

Six years later, still not sure what the right answer is here, but this should get resolved definitively.

sayrer avatar May 01 '21 22:05 sayrer