Tal Tal - 3 months ago 12
Jade Question

Transfer value from Node JS to Jade

i am trying to transfer an argument from node js to Input Tag in my Jade page,
here is my code:

Node js :

res.render("index" ,{title: userName});


Jade Page (which called 'index') :

.input
input(type='text', value= #{title} , maxlength='15')


What i am doing wrong here ?

Here is my error message :


48| input(type='text', value= #{title} , maxlength='15')

Unexpected token ILLEGAL
at Function (native)


`

Answer

You are using an escaped string interpolation like described here: http://jade-lang.com/reference/interpolation/ This doesn't work as attribute, you have to use buffered code, like described here: http://jade-lang.com/reference/code/

It would look like this:

// escaped code
.input
    input(type='text', value= title ,  maxlength='15')
// unescaped code
.input
    input(type='text', value!= title ,  maxlength='15')

If you want to use an object for your attributes, you can handle it with input&attributes(object), like descibed here: http://jade-lang.com/reference/attributes/#and-attributes

Node.js

res.render("index" ,{
   input: {
      type: 'text',
      title: userName, 
      maxlength: '15',
      name: 'myInputName'
   }
});

Jade:

.input
    input&attributes(input)

Result of both:

<div class="input">
   <input type="text" value="username" maxlength="15" name="myInputName" />
</div>

Here is a workinh Pen without Node as example in Jade: http://codepen.io/pure180/pen/oLPGJy

Comments