Skip to content

conditional boolean attributes #111

Description

@iceripple

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions