Search code examples
node.jsconditional-statementspug

Same line Jade If Statement


I would like to be able to do an if statement on the same line in jade

 input(type='text', name='email', placeholder='Email', value='#{user.email}')

user.email should only display if it is defined.

 //For example:
 input(type='text', name='email', placeholder='Email', value='if user.email #{user.email}')

Solution

  • The values of attributes are JavaScript expressions, allowing you to reference variables without any additional syntax and is also why literal (string) values require quotes.

    input(type='text', value=user.email)
    

    If the value assigned to the attribute is null or undefined, Jade will already exclude it from the resulting markup:

    - user = { email: null }
    input(type='text', value=user.email)
    
    <input type="text"/>