RejectedSoftware Forums

Sign up

best way to do radio buttons in diet?

We all know that the web is poorly designed for application development.
I'm struggling with the concept of which radio button should be checked
based on a D variable.

Everyone here probably knows this, but in html, the way you say which
radio button is checked is to add a "checked='anything'" to the radio
button.

Let's say you have a boolean value, and you want to check "yes" or "no"
depending on that value. I'm looking at this code I wrote, and it's awful:

     #b Boolean is set:
       - if(mybool)
         input#b_yes(type='radio',name='b',value=1,checked='1')
         label(for='b_yes') Yes
         input#b_no(type='radio',name='b',value=0)
         label(for='b_no') No
       - else
         input#b_yes(type='radio',name='b',value=1)
         label(for='b_yes') Yes
         input#b_no(type='radio',name='b',value=0,checked='1')
         label(for='b_no') No

Can diet handle adding or not adding an attribute based on a D boolean?
That would make this a LOT better.

You can imagine a set of radio buttons that is larger being even more
horrible, though looping and nesting the if statement makes it a little
more palatable (but not by much).

-Steve

Re: best way to do radio buttons in diet?

On 6/16/17 8:41 AM, Steven Schveighoffer wrote:

Can diet handle adding or not adding an attribute based on a D boolean?
That would make this a LOT better.

I will note that pug handles this via:

input(type='radio' name='b' value='0' checked=false) // checked is
omitted from the html

But diet doesn't let me do this (I think):

input(type='radio',name='b',value='0',checked=#{mybool}) // Error:
expression expected, not '#' (and a whole bunch of other errors

-Steve

Re: best way to do radio buttons in diet?

On 6/16/17 8:55 AM, Steven Schveighoffer wrote:

On 6/16/17 8:41 AM, Steven Schveighoffer wrote:

Can diet handle adding or not adding an attribute based on a D boolean?
That would make this a LOT better.

I will note that pug handles this via:

input(type='radio' name='b' value='0' checked=false) // checked is
omitted from the html

But diet doesn't let me do this (I think):

input(type='radio',name='b',value='0',checked=#{mybool}) // Error:
expression expected, not '#' (and a whole bunch of other errors

Hah, got it to work. Thanks for the detailed spec at
https://github.com/rejectedsoftware/diet-ng/blob/master/SPEC.md !

input(type='radio',name='b',value='0',checked=mybool)

That's actually pretty easy :) I had no idea you could just put a D
expression in there...

-Steve