fhd / clostache

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

Strings now repeat according to length #30

Open JulianBirch opened 11 years ago

JulianBirch commented 11 years ago

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.

fhd commented 11 years ago

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.

cunger commented 10 years ago

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...

JulianBirch commented 10 years ago

Think ultimately the problem is that ruby doesn't expose string as a sequence, and Clojure does.

On Wednesday, October 1, 2014, Christina Unger notifications@github.com 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.

aroemers commented 9 years ago

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.