Pavel Pavel - 28 days ago 15
Jade Question

Jade conditional with select options

How to set the

option
(of
select
) to
selected
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}

Answer

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
  else
    option(value=code)= name

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