Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJan Peter Mamuric
7,093 PointsIn the video, The “response.render” Method, you have @ 3 min into the video you have res.render ('post', {post: post});
In the video, The “response.render” Method, you have @ 3 min into the video you wrote res.render ('post', {post: post});
I'm having trouble differentiating which one uses the post is the "post.jade" file and which post is the variable "var post = posts[title];"
need help.
1 Answer
jsdevtom
16,963 PointsHi, I also thought this was very confusing. And logically, I don't think this line of code would be considered a best practice either due to its triple use of the word.
Others, please correct me if I am wrong, but I believe that the first 'post' in res.render('post', {post: post});
is telling the app to render the post.jade in the response. It's doing this thanks to these two lines of code:
app.set('view engine', 'jade');
app.set('views', __dirname + '/templates');
The second 'post' in res.render('post', {post: post});
is declaring a property inside of the object which is being provided as the second parameter in the response.render method.
The third 'post' in res.render('post', {post: post});
is the value of this object. This third 'post' refers to the var post = posts[title];
How does the app know which one is which? Because res.render's first argument is always the view (which has been defined in the two lines of code above) so it looks for a view and not a variable. And res.render knows that the second 'post' is a property of an object because it comes after a '{'. It knows that the third post is a variable because it is not a string nor a number nor a boolean value, so it goes to look for that variable.
For more on res.render
please see the following: http://expressjs.com/en/api.html#res.render
If you have any more questions, please ask :-)
Jan Peter Mamuric
7,093 PointsJan Peter Mamuric
7,093 Pointsgreat! Thank you so much. That makes a lot more sense now, I was playing around with the code, and it make it easier when I changed the "second post" property to a different name. For future reference to anyone reading this my code looks like this just for clarification, however I put it back to original to keep my code clean.
('post', {post : post}); 1st post: 'post.jade' 2nd post: anyPropertyName 3rd post: var post = posts[title]
Laurie Gray
23,119 PointsLaurie Gray
23,119 PointsAwesome thanks :)
Sebastiaan Hols
19,520 PointsSebastiaan Hols
19,520 PointsThanks for the explanation!
I kind of understood this by reading the Express docs, but this clears it all up.
Note to self: never use the same word, meaning three separate things, in two separate files, in one line of code...