diff --git a/README.md b/README.md index 562c0c3..70f2cfa 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,28 @@ Output: ``` Hello, World! Hello, Felix! +``` + +Functions can also render the text given to them if they need to do something more complicated. + +Template: + +```mustache +"{{#people}}Hi {{#upper}}{{name}}{{/upper}}{{/people}}" +``` + +Data: +```clj +{:people [{:name "Felix"}] + :upper (fn [text] + (fn [render-fn] + (clojure.string/upper-case (render-fn text))))} +``` + +Output: + +``` +Hello FELIX ``` Development diff --git a/src/clostache/parser.clj b/src/clostache/parser.clj index 67a052d..8ca930b 100644 --- a/src/clostache/parser.clj +++ b/src/clostache/parser.clj @@ -341,7 +341,10 @@ (:body section)) (if section-data (if (fn? section-data) - (section-data (:body section)) + (let [result (section-data (:body section))] + (if (fn? result) + (result #(render-template % data partials)) + result)) (let [section-data (cond (sequential? section-data) section-data (map? section-data) [section-data] (seqable? section-data) (seq section-data) diff --git a/test/clostache/test_parser.clj b/test/clostache/test_parser.clj index 8fcdca5..a0f556a 100644 --- a/test/clostache/test_parser.clj +++ b/test/clostache/test_parser.clj @@ -113,7 +113,11 @@ (deftest test-render-lambda-with-params (is (= "Hello, Felix" (render "{{#greet}}Felix{{/greet}}" - {:greet #(str "Hello, " %)})))) + {:greet #(str "Hello, " %)}))) + (is (= "Hi TOM Hi BOB " + (render "{{#people}}Hi {{#upper}}{{name}}{{/upper}} {{/people}}" + {:people [{:name "Tom"}, {:name "Bob"}] + :upper (fn [text] (fn [render-fn] (clojure.string/upper-case (render-fn text))))})))) (deftest test-render-resource-template (is (= "Hello, Felix" (render-resource "templates/hello.mustache" {:name "Felix"}))))