clostache icon indicating copy to clipboard operation
clostache copied to clipboard

Allow lambda sections to render text

Open simonl2002 opened this issue 11 years ago • 4 comments

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"

simonl2002 avatar May 14 '13 22:05 simonl2002

Agreed, that sounds useful.

fhd avatar Jun 05 '13 12:06 fhd

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.

simonl2002 avatar Jun 05 '13 12:06 simonl2002

Any word on this? Would be very nice...

darkone23 avatar May 03 '14 08:05 darkone23

Sorry, slipped off my radar :disappointed: Reviewed the pull request, pretty much ready to merge.

fhd avatar May 03 '14 21:05 fhd