davidjamesstone / superviews.js

Template engine targeting incremental-dom
http://davidjamesstone.github.io/superviews.js/playground/
246 stars 15 forks source link

Adding attributes conditionally #8

Closed zandaqo closed 8 years ago

zandaqo commented 8 years ago

Hi,

I'd like to add attributes depending on a condition, something like:

<input type="checkbox" { isChecked ? 'checked' : ''}>

which should add checked attribute to the input if isChecked is true:

<input type="checkbox" checked>

That doesn't seem to be possible with the current implementation. I'm wondering if there is another, declarative way to do it, and if not, whether it was left out because of some architectural constraints or simply was not previously considered?

davidjamesstone commented 8 years ago

Hi,

If the expression evaluates to either null or undefined, the attribute should be omitted. This is undocumented ATM.

Try this:

<input type="checkbox" checked="{ isChecked ? 'checked' : null}">
or
<input type="checkbox" checked="{ isChecked ? '' : null}">
zandaqo commented 8 years ago

Yes, that works. Thanks!

Though I would recommend at least adding this into the examples in Readme to avoid possible confusion over it.

dak commented 8 years ago

This appears to work for undefined but not for null.