twitter / hogan.js

A compiler for the Mustache templating language
http://twitter.github.io/hogan.js
Apache License 2.0
5.14k stars 431 forks source link

render is undefined #222

Open rouberg opened 9 years ago

rouberg commented 9 years ago

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) + "" } } }

ex1st commented 9 years ago

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

{
  "wrapped": function() {
    return function(text) {
      return Hogan.compile(text).render(this);
    }
  }
}
Jerska commented 9 years ago

See #199

sayrer commented 9 years ago

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

Jerska commented 9 years ago

@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.

sayrer commented 3 years ago

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