Patrick
Patrick

Reputation: 8063

Inline condition

  - 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.

Upvotes: 6

Views: 6319

Answers (4)

A-Sharabiani
A-Sharabiani

Reputation: 19329

You can short circuit as follows:

input(type="text", name="person[Name]", value="#{person && person.Name}")

Upvotes: 0

kkites
kkites

Reputation: 349

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)

Upvotes: 4

Romain Meresse
Romain Meresse

Reputation: 3044

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')
...

Upvotes: 6

realguess
realguess

Reputation: 1151

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))

Upvotes: -1

Related Questions