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 trialNicolas Hampton
44,638 PointsSeparation of parts was a little unclear
So this class has been pretty darn good so far, so let me start with thanks. But in this lesson, I noticed something I found a little confusing. So many things by this point are named 'post' that it was a little unclear as to what was connected to what. It wasn't until I broke a few things on purpose until I understood the workings of the render method, which seems to be similar to Angular injection:
When we pass the parameter {post : post} like this
res.render('post', {post: post});
, it almost looks like post is something built into express as a native method. But if we change {post:post} to {anything:post}, we're changing the key that the values can be called by in our post.jade template, nothing more. So in the template, if we change
#{post.title}
to
#{anything.title}
and so on in the template, everything will still work. The Key in the key value pair that we pass to the render method is arbitrary, but the value must be our data set, in this case posts.json, which we've stored in the variable 'post'. See how that's a little confusing when we use the name 'post' for everything? (especially since post is also a REST term).
So the key/value pair we're passing to res.render() is an arbitrary Key name we can use to refer to the dataset we pass as its value. I hope that helps anybody who is experiencing the same questions as I. Happy coding!
Nicolas
5 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsYeah you just comma separate them. It's just a regular object literal, so the normal rules about key names apply:
res.render('post', {blogPost: post, anotherKey: 'string value'});
I'd probably avoid key names with spaces though, as I don't know how you'd use them in the template...
Anthony Buzz
1,279 PointsThis helps clear things up for me. Thank you for this post on posting posts.
Iain Simmons
Treehouse Moderator 32,305 PointsThere are so many questions on the forums where the confusion has been caused by the naming choices of variables/parameters!
Naming things in a logical way continues to be one of the biggest challenges facing developers!
Liu Bing
9,071 PointsI'm so confused with all those 'post's, please don't name your variable and template and json file that similar, guys.
Nicolas Hampton
44,638 PointsDoes anyone know if we can pass in multiple key:value pairs, and what that syntax may look like?
Antonio Jaramillo
15,604 PointsAntonio Jaramillo
15,604 PointsGreat comment. It helped me out a lot. Just to sure things up even more, I would add that we didn't assign our data set posts.json to var post, but rather we assigned posts.json[title] to it.