Search code examples
javascripthtmlnode.jspug

NodeJS - Pug - Add variable from database in input value


I'm trying to show the value from #{product.name} in a textfield. This is my code, but it doesn't work

label Name
input(type='text', placeholder='Name', name='name' value='#{product.name}')

This is my result:

Screenshot

Can someone tell me how to do this?


Solution

  • Assuming that you are using a newer version of pug, string interpolation in attributes has been removed from the language in favor of ES6-template-strings.

    This means you are in theory supposed to use syntax like this now:

    input(type='text', placeholder='Name', name='name' value=`${product.name}`)
    

    That being said, your example does not require using interpolation at all, and you could simply be passing the variable's value:

    input(type='text', placeholder='Name', name='name', value=product.name)