HamlPy icon indicating copy to clipboard operation
HamlPy copied to clipboard

conditional boolean attributes

Open iceripple opened this issue 13 years ago • 5 comments

You often need to set boolean attributes (checked, selected,disabled) conditionally in a template

in Haml you can do something like:

%input{:selected => false}

which will evaluate to:

<input>

of course, Hamlpy cannot do this as it doesn't have access to runtime variables. a line like:

%button{'disabled' : ={button_disabled}}

wlll always result in a disabled button, whatever the value of button_disabled

Any better solutions than the ones I have come up with?

-if next_disabled
    %button{'disabled':'disabled'} Next...
-else
    %button Next...

very un-dry, especially if the element has a lot more attributes Or just insert raw django template stuff:

<button {% if next_disabled %}disabled="disabled"{% endif %}>Next…</button>

being somewhat counter-intuitive when you have elected to use Haml

iceripple avatar Nov 27 '12 15:11 iceripple

Take a look at my fork: https://github.com/D3X/HamlPy I added support for conditional attributes. It's not production-ready yet, i hope to clean it up soon. Don't forget to:

  1. install pyparsing if you want to use this fork
  2. tell me what's broken :)

D3X avatar Dec 27 '12 11:12 D3X

I would love to see this supported, @D3X any chance the change you made to your fork could easily be submitted as a pull request to the parent repo? (I could use your fork personally, but not on any of the collaborative projects I'm working on that could use this feature.)

peterbehr avatar Jun 23 '14 03:06 peterbehr

:+1:

skatenerd avatar Mar 12 '15 07:03 skatenerd

+1

jlward avatar Mar 23 '17 18:03 jlward

@peterbehr @skatenerd @jlward Better late than never, I guess: https://github.com/jessemiller/HamlPy/pull/172 Now you have to push @jessemiller to merge it upstream :)

D3X avatar Mar 23 '17 22:03 D3X