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

A variable which is boolean false is not rendered in the template #20

Closed
philc opened this issue Jun 27, 2014 · 5 comments
Closed

A variable which is boolean false is not rendered in the template #20

philc opened this issue Jun 27, 2014 · 5 comments

Comments

@philc
Copy link

philc commented Jun 27, 2014

(stencil/render-string "{{a}}" {:a true}) => "true"
(stencil/render-string "{{a}}" {:a false}) => ""

; Note that (str false) => "false"

This is very surprising. If it's intended behavior, why?

@davidsantiago
Copy link
Owner

I think the answer is, I'm not sure. I reread the mustache "spec" just now, and as far as I can tell, it only specifies the way that strings, integers, and decimals should render when used as a replacement. It says nothing about booleans, so what should happen there never gets specified. Of course, the current output is indeed surprising, they should both render the same way. The proximate cause is that the code to look up values for the replacement basically pulls keys out of the map and if it gets a "nil/false" value it considers the key not to be there. The problem is, with the spec saying nothing about this one way or the other, I'm not sure if I should make booleans never render, or always render. I'm inclined towards the former, since I don't think the template engine should be in the business of deciding how any unspecified piece of data it's given should look when rendered, and then trying to make it render at all costs with a guess.

@philc
Copy link
Author

philc commented Jun 27, 2014

I think most people would expect bools to automatically get serialized as strings. If you decide not to support bools as strings, please throw an exception when they're evaled as strings in a template, so that it's very clear that an unsupported data type is being used.

@philc
Copy link
Author

philc commented Jun 27, 2014

For another point of comparison, we could look at the most popular mustache library to see what its behavior is.

@davidsantiago
Copy link
Owner

Yeah, I don't think something's popularity is a great argument for it, nor are philosophical arguments about what an "average programmer" would "expect." But for a few small reasons (breakage potential, "de facto standard" consistency you point out), it sure seems like the path of least resistance while still consistent with the minimal guidance from the "spec." So I will make the booleans both render.

I still think it is wisest to pre-render to a string anything that is not given a specified behavior; some things there just aren't a good answer for. How should a nil render if you give it as {:a nil}? I have no idea. (str nil) gives you "", so that's what you'll get, but that doesn't seem like what someone who fed that context map in would "expect" if you analogize it with what {:a false} (will soon) render as. It's also of rather limited use. For example, Mustache will never let you render "False" at the front of a sentence from a false value, if that's what you need. But this is just another reason I've become convinced over time that Mustache is just not a powerful-enough language for heavy duty use as a template engine.

@davidsantiago
Copy link
Owner

Fixed in the 0.3.4 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants