Inline condition
Asked Answered
K

4

6
  - 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 an option select and I don't want to do a conditional statement for 30+ values to select the right option.

Koheleth answered 19/12, 2011 at 16:10 Comment(0)
P
4

conditional statement should do

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

however, by design we can always pass a person? this way there is no comparison required. Code would be something like

input(type='text', name='person[Name]', value= person.name)
Payton answered 21/12, 2011 at 11:0 Comment(1)
That is of course brilliant. Passing an empty object to prevent "undefined" errors, no need for mixing or conditionals. I used JavaScript to select the option in the list, as the conditional statement didn't work.Koheleth
S
6

You could 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')
...
Severen answered 20/12, 2011 at 12:43 Comment(1)
Hmm, not bad. I have to see if that works for option-select's as well. Thanks!Koheleth
P
4

conditional statement should do

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

however, by design we can always pass a person? this way there is no comparison required. Code would be something like

input(type='text', name='person[Name]', value= person.name)
Payton answered 21/12, 2011 at 11:0 Comment(1)
That is of course brilliant. Passing an empty object to prevent "undefined" errors, no need for mixing or conditionals. I used JavaScript to select the option in the list, as the conditional statement didn't work.Koheleth
K
0

You can short circuit as follows:

input(type="text", name="person[Name]", value="#{person && person.Name}")
Kilowatthour answered 2/11, 2018 at 23:0 Comment(0)
M
-1

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

input(type='text', name='person[Name]', value= person && typeof(person))
Myotonia answered 19/12, 2011 at 17:10 Comment(1)
Unfortunately, Jade shows a 500 ReferenceError if person is not defined. If person is defined, it says "string" instead of the value in the input box. So that doesn't work.Koheleth

© 2022 - 2024 — McMap. All rights reserved.