clostache
clostache copied to clipboard
Allow lambda sections to render text
From the mustache manual:
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.
After looking at the code appears that if a section is a function we apply it to the body
(if (fn? section-data)
(section-data (:body section))
...)
Which means there is no way for the lambda to render that text. Perhaps like the javascript mustache implementation a additional parameter should be passed into the lambda (https://github.com/janl/mustache.js#functions) for use in rendering the section contents if necessary.
An example:
(def template "{{#people}}Hi {{#upper}}{{name}}{{/upper}} {{/people}}")
(def data
{:people [{:name "Tom"}, {:name "Bob"}]
:upper (fn [text render-fn] (clojure.string/upper-case (render-fn text)))})
(render template data)
=> "Hi TOM Hi BOB"
Agreed, that sounds useful.
I did a quick test implementation, https://github.com/simonl2002/clostache/compare/master...lambda_render
What I did was if a section returned a lambda, we call that lambda with a rendering function as the argument. I did it this way because passing the render function directly to a section lambda is not really compliant with the spec and would break test cases.
Any word on this? Would be very nice...
Sorry, slipped off my radar :disappointed: Reviewed the pull request, pretty much ready to merge.