fhd / clostache

{{ mustache }} for Clojure
GNU Lesser General Public License v3.0
318 stars 62 forks source link

Nesting function calls #18

Closed filipesabella closed 12 years ago

filipesabella commented 12 years ago

Hello there,

Given this data: {:format #(format-date %)} {:user {:created-at (now)}}

And this template: {{#format}}{{user.created-at}}{{/format}}

The "format-date" function receives {{#user}}{{created-at}}{{/user}}.

Is this expected behavior? Shouldn't the parser parse the expressions inside out?

fhd commented 12 years ago

That does indeed look like a bug, I'll look into it later today.

fhd commented 12 years ago

I've tried to create a minimal test case that verifies the problem. Here it is:

(render "{{#format}}{{user.created-at}}{{/format}}" {:format #(str %) :user {:created-at 1}})

However, it does return "1", which would be correct.

filipesabella commented 12 years ago

Took me a few tries to figure this one out.

Try this: (render "{{#format}}{{user.created-at}}{{/format}}" {:format #(str/replace % "created-at" "hello") :user {:created-at 1 :hello "there"}}) It prints "there" instead of 1.

The argument the function receives is {{#user}}{{created-at}}{{/user}} which eventually gets parsed out to its value after the function has been called.

The function in your example, that just prints back the value it received, actually works - but because of the reasons above.

Another test would be: (render "{{#format}}{{user.created-at}}{{/format}}" {:format #(inc %) :user {:created-at 1}})

Which will throw a: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number

fhd commented 12 years ago

Oh, that would be a bug then, thanks for reporting :)

I'll get on it.

fhd commented 12 years ago

I'm sorry, but this turned out to be a feature - according to the Mustache spec.

I just fixed this, slapped myself in the face for implementing it like that in the first place, ran the spec tests and saw them fail :(

FAIL in (test-lambdas) (test_specs.clj:36)
Section - Lambdas used for sections should receive the raw section string.
Template: "<{{#lambda}}{{x}}{{/lambda}}>"
Data: {:x "Error!", :lambda "(fn [text] (if (= text \"{{x}}\") \"yes\" \"no\"))"}
expected: (= (:expected spec-test) (render template data partials))
  actual: (not (= "<yes>" "<no>"))

Thanks for taking the time to report and reproduce this.

filipesabella commented 12 years ago

Oh well :)

Thanks a lot for taking the time to look into this, and also thanks for the awesome lib.