Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lambda sections to render text #33

Merged
merged 3 commits into from
May 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/clostache/parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion test/clostache/test_parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))))
Expand Down