creationix / haml-js

Haml ported to server-side Javascript. This is a traditional server-side templating language. Tested with node-js
MIT License
900 stars 109 forks source link

Conditional `selected` at runtime #48

Open dnagir opened 13 years ago

dnagir commented 13 years ago

So how do you generate (or not) selected attribute with HAML similar to this:

#something{selected: visible}

When I do: Haml.compile('#something{selected: visible}')

I get the code generated:

"<div selected=\"" + undefined(visible) + "\" id=\"something\">" + 
"</div>"

This includes undefined(visible) which is invalid JavaScript. Am I doing something wrong here?

aaronblohowiak commented 13 years ago

the undefined() is a bug and will be fixed (probably later tonight.) -- it will instead output the html escaping function, by default html_escape().

Can I ask why you are using Haml.compile instead of Haml() ? The latter returns the executable function, with the appropriate with(locals){...}.

Thanks!

dnagir commented 13 years ago

Ok. Thanks for the upcoming fix to this.

I was using compile to see what gets generated to understand better how haml-js works.

But I might also use compile to generate JS code on the server Currently I am using Haml(".content") then execute toString(). And then strip out the function name.

This seems to be a bit crazy to do.

Additionally I don't like that Haml function wraps the template code in catch block. I would rather prefer to see where exactly the error happened instead of silently rendering the default error template.