clostache
clostache copied to clipboard
Strings now repeat according to length
Imagine you're working on
(render "{{#x}}0{{/x}}" { :x "hello" })
This prints "XXXXX". I'm inclined to think that it should print "X", and I imagine the equivalent code does in ruby (don't have it to hand). I'll send through a pull if we agree it's a bug.
I'm not so sure, to me this looks like you want it to loop over the string hello. But both the Ruby and JS implementations print it just once, so I don't mind doing it the same way.
I also stumbled upon this. Since the mustache manual says about sections that "When the value is non-false but not a list, it will be used as the context for a single rendering of the block", I would expect to have the string printed only once.
I actually use {{#x}}{{.}}{{/x}} with { :x "some string" } quite often to check whether a key exists and if so, print it (and do something else if it doesn't). The only alternative I can think of is to extend the context with something like { :has-x true } for every relevant key :x, but this seems quite tedious...
Think ultimately the problem is that ruby doesn't expose string as a sequence, and Clojure does.
On Wednesday, October 1, 2014, Christina Unger [email protected] wrote:
I also stumbled upon this. Since the mustache manual says about sections that "When the value is non-false but not a list, it will be used as the context for a single rendering of the block", I would expect to have the string printed only once.
I actually use {{#x}}{{.}}{{/x}} with { :x "some string" } quite often to check whether a key exists and if so, print it (and do something else if it doesn't). The only alternative I can think of is to extend the context with something like { :has-x true } for every relevant key :x, but this seems quite tedious...
— Reply to this email directly or view it on GitHub https://github.com/fhd/clostache/issues/30#issuecomment-57425568.
Sent from an iPhone, please excuse brevity and typos.
A small workaround for now, wrap your Strings in an Object:
(defn- string-object
"Wraps a String in an Object that returns the given String when
.toString is called. Wrapping a String like this prevents clostache
seeing a String as a collection."
[s]
(when s
(reify Object
(toString [this] s))))
This turns the String into expected behaviour, just like Ruby, JavaScript and Java.