The internal state of jade

- if (typeof(person) == 'undefined') input(type="text", name="person[Name]") - else input(type="text", name="person[Name]", value="#{person.Name}") 

Is there any way to write this inline? I have a select option, and I don’t want to do a conditional statement for 30+ values ​​to select the correct one.

Thanks!

+6
source share
3 answers

conditional statement must do

 input(type='text', name='person[Name]', value= (person?(person.name?person.name:''):'')) 

however, in design we can always convey a person? therefore, no comparison is required. The code will be something like

 input(type='text', name='person[Name]', value= person.name) 
+4
source

You can use mixins

 mixin safeInput(person, property) - if (typeof(person) == 'undefined') input(type="text", name="person[#{property}]") - else input(type="text", name="person[#{property}]", value="#{person[property]}") 

Then

 mixin safeInput(person, 'Name') mixin safeInput(person, 'Email') ... 
+6
source

When undefined or null , the attribute will not be shown. This should work:

 input(type='text', name='person[Name]', value= person && typeof(person)) 
-1
source

Source: https://habr.com/ru/post/904093/


All Articles