Search code examples
javascriptpugdhtml

Pug (Jade) add presence of attribute inline dynamically


Depending on a variable, the style attribute can be present or not.

label(for='ff' 
  class='ff1' 
  varFlag != undefined ? eval(style= 'letter-spacing: -5px;') : eval('')
)

-- that does not work. And the following code adds empty style, that is not accaptable:

label(for='ff' 
  class='ff1' 
  style= varFlag != undefined ? 'letter-spacing: -5px;' : ''
)

How can we have an attribute depending on a condition?

Checked similar questios - found nothing relevant.


Solution

  • One approach would be to have two separate elements for that scenario:

    if !varFlag
      label(for='ff' class='ff11')
    else 
      label(for='ff' class='ff1' style="letter-spacing: -5px")
    

    Or if you want to get really fancy, you could use a mixin, but that might be more heavy lifting than is needed.