How do I render a page in Node.JS with hogan-express

675 views Asked by At

How do I render a page in node (using a router) which just passes a value to the template engine for me to use?

So for instance (this is the real code):

var apiRouter = express.Router();

 apiRouter.get('/myPosts', function(req, res){
    userPostsModel.findOne({'profileID':req.session.facebookProfileId}, function(err, userPosts) {
            if(userPosts) {
                res.json({
                    posts       :   userPosts.posts
            });
            } else {
                console.log('You do not have any posts');
            }
        })
})

And the template engine part (html file):

<!doctype html>
<head>
    <meta charset="UTF-8">
    <title>My Posts</title>
</head>
<body>
    <p><a href="/post?id={{posts.[0].postID}}">Post 1</a></p>
    <p><a href="/post?id={{posts.[1].postID}}">Post 2</a></p>
</body>
</html>

I am expecting this to render the page but instead, I am getting this (on the webpage):

{"posts":["12345667"]}

For my template engine, I am using 'hogan-express', as below:

app.engine('html', require('hogan-express'));
app.set('view engine', 'html');

I know the res.json may be wrong here but I am not getting anywhere with this. The documentation for node and the routers is all over the place!

1

There are 1 answers

0
Sean On BEST ANSWER

As @Molda has answered in the comments, below, it should be like this:

res.render('template-name', { posts:userPosts.posts});

Tried it and worked for me.