Jade dynamic variables

Asked

Viewed 146 times

4

Is there any way to render dynamic variables in jade with expressjs, when I say dynamic is to change in a loop without Reload in the page.

...
    app.set('view engine','jade');    
    app.get('/',function(req,res}{
          res.render('index.jade',{name:'Rafael'})
        })

The intention is to refresh the res.render altering the {name:Rafael} for {name:Maria}(example) keeping the generated layout static. Got how? with socket or ajax?

1 answer

2

To do what you want you have to use AJAX.

Choose a route to AJAX and render the content you need in a string. To do this, pass a callback to .render:

app.get('/', function(req, res) {
    res.render('/partials', {name:'Rafael'}, (err, html) => {
        if (err) return console.log(err);
        var str = encodeURIComponent(html);
        res.send({html: str});
    });
});

So send a JSON back to the Browser and use the HTML string in the content you need.

In the example I used {name:'Rafael'} but you can give Jade whatever data you want clear.

Note: Jade is now called Pug. Jade has been abandoned. You can read more about it here.

  • I tried to implement the idea soque did not work very well or did not understand the idea, this returned html will not replace the tmp that has already been rendered?

  • @Rafaelvergopolan I will try later to add an example of ajax if you do not know how to implement this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.