How to get object's value in Pug?

6.5k Views Asked by At

I'm using Node.js to render a page using Pug. My JavaScript code:

router.get('/', function(req, res, next) {
  res.render("index",{
    title:"首页",
    user:{name:"luo",age:19}
  });
});

My Pug code:

script.
    window.user = #{user}

But the result is like this:

<script>window.user = [object object]</script>

How to get the object's value correctly?

1

There are 1 best solutions below

0
Michał Perłakowski On

Use JSON.stringify() and change # to ! to prevent the quotes from being escaped:

script.
    window.user = !{JSON.stringify(user)}

See Pug documentation for interpolation.