Pavel Pavel - 6 months ago 55
Jade Question

Jade conditional with select options

How to set the

) to
to match the current language of the page? Is there a way to inline it and simplify it something like this:

(value="en" #{Locale}==='en' ? ',selected="selected"': '')

I have tried some answers on this site, but they do not seem to work. Thank you.

This is the view:

if(#{Locale} ==='en')
option(value="en", selected="selected") #{English}
option(value="bg") #{Bulgarian}
else if(#{Locale} === 'bg')
option(value="en") #{English}
option(value="bg",selected="selected") #{Bulgarian}


Adapted from this answer, you could create a mix-in that handles the logic for you:

mixin lang-option(code, name)
  if (Locale === code)
    option(value=code, selected="selected")= name
    option(value=code)= name

+lang-option('en', English)
+lang-option('bg', Bulgarian)