brent brent - 6 months ago 76
Node.js Question

Insert Image using Jade and NodeJS

how can I use an object that ive passed through to jade within an image, im also using mongodb to hold the data.

Currently this is my code:

db.collection('blogposts', function(err, collection) {
if (err) throw err;
collection.find().toArray(function(err, docs) {
if (err) throw err;
res.render('table', { title: 'Blog Posts', tab: "list" , blogposts: docs });
});
});


So I have the nodejs passing through a mongodb collection through to jade,
Then within Jade I have:

div.span9
table.table.table-bordered.table-striped.noborder

each row in blogposts
tr
td
div.blogtitle #{row.Title}
br
div.blogheading #{row.Heading}
br
div.namedate #{row.Namedate}
br
div.imagetable
img(src='')
br
div.blogposts #{row.Posts}
br
div.blogtags Tags: #{row.Tags}


And what im trying to do is use #{row.Image} within the actual img(src='') as the source.

It appears that i must use some other syntax or something to use it within the source as just putting it in doesnt work.
Thanks

Answer

Just do img(src= "http://" + row.Image)

Jade will treat the src attribute value as a javascript expression, evaluate it and render the HTML as you would expect.

Comments