clostache icon indicating copy to clipboard operation
clostache copied to clipboard

Strings now repeat according to length

Open JulianBirch opened this issue 11 years ago • 4 comments

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.

JulianBirch avatar May 10 '13 19:05 JulianBirch

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.

fhd avatar May 13 '13 05:05 fhd

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

cunger avatar Oct 01 '14 06:10 cunger

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.

JulianBirch avatar Oct 01 '14 07:10 JulianBirch

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.

aroemers avatar Feb 28 '15 01:02 aroemers