When adding radio buttons and checkboxes to my pre-filled form I find myself having to duplicate code.
Ideally I want to declare via a variable if the input field should be selected or not. Like e.g.:
- checked = false # or `true`, `false`, `nil`, `""`
%input{:type => "radio", :name => "foo", :value => "bar", :checked => checked}= "foo"
%input{:type => "checkbox", :name => "foo", :value => "bar", :checked => checked}= "foo"
But the result is that whatever value I put into the checked
variable it results in the input field ends up selected.
My solution results in lots of lines of duplicate code:
- checked = false # or `true`, `false`, `nil`, `""`
- if true
%input{:type => "radio", :name => "foo", :value => "bar", :checked => true}= "foo"
- else
%input{:type => "radio", :name => "foo", :value => "bar"}= "foo"
- if true
%input{:type => "checkbox", :name => "foo", :value => "bar", :checked => true}= "foo"
- else
%input{:type => "checkbox", :name => "foo", :value => "bar"}= "foo"
Anyone who know a better way?