I'm having a serious trouble trying to discover how to do a certain thing that i don't know how to name/describe it correctly in nodejs, I've learned some concepts and basics of nodejs and i'm already messing up with express and jade but i stll can't figure out what's the equivalent of this on a .jade file, ignore the html markup. ( What i'm aiming to do is printing the results of a sql fetch )
<body>
<p><? print "Hello World"; ?></p>
</body>
My app.js
var express = require('express'),
app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.get("/", function(req, res) {
res.render('page.jade', {
title: 'My Page'
});
});
app.use('/static', express.static(__dirname + '/static'));
app.listen(app.get('port'), function() {
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});
PS: I'm sorry if my question is too mediocre, I've searched but could't find anything that helped me.
Any object you pass with render gets passed into the view, which would be your jade file. You can then render the object property using = after the tag or #{} inline.
res.render('page', {
title: 'My Page',
name: 'Ben'
});
html
head
title= title
body
p My name is #{name}
Output would be:
<html>
<head>
<title>My Page</title>
</head>
<body>
<p>My name is Ben</p>
</body>
</html>
To add on to the other answers, you can also do if/else statements in your jade file
App/Router
res.render('view',{
"error": "ExampleError!"
});
View
if error
p An error occurred: #{error}
else
p All is well
Output
<p>An error occurred: ExampleError!</p>