Currently, in order to implement filters/lambdas you have to know and do a lot. You have to:
include hogan.js in your app
know the strange "lambda factory"-ish style of using a function that returns a function
realize that what you get as "text" in the function call is the pre-rendered/compiled template snippet
know the hogan.js API for compiling and rendering the template snippet
pass around variables/context (especially when trying to operate on objects within an array loop)
This pull request encapsulates all of that for you so you can simply define your lambdas as functions that should be run on the processed template snippet. For example:
app.get '/', (req,res)->
res.locals = myDefaultLabel: "oops" # here to show a little of how scoping works
res.render 'template',
message: 'This is a message. HERE.'
mylist: [{label: "one", num: 1},{label: "two", num: 2},{num: 3}]
lambdas:
lowercase: (text) ->
return text.toLowerCase()
reverseString: (text) ->
return text.split("").reverse().join("")
template:
<p>Lowercase <strong>{{message}}</strong>: {{#lambdas.lowercase}}{{message}}{{/lambdas.lowercase}}</p>
<ul>
{{#mylist}}
<li>{{num}}: {{label}} is {{#reverseString}}{{label}}{{#reverseString}} in reverse.</li>
{{/mylist}}
</ul>
rendered html:
<p>Lowercase <strong>This is a message. HERE.</strong>: this is a message. here.</p>
<ul>
<li>1: one is eno in reverse.</li>
<li>2: two is owt in reverse.</li>
<li>3: oops is spoo in reverse.</li>
</ul>
You can see this in action in the example app. I also wrote some tests (mocha+supertest) that exercise this new feature within the app. README was also updated to reflect the new feature.
Currently, in order to implement filters/lambdas you have to know and do a lot. You have to:
This pull request encapsulates all of that for you so you can simply define your lambdas as functions that should be run on the processed template snippet. For example:
template:
rendered html:
You can see this in action in the example app. I also wrote some tests (mocha+supertest) that exercise this new feature within the app. README was also updated to reflect the new feature.