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 trialGinger Williams
6,409 PointsStill getting {{}} for everything except the badges
I am only getting the results for the badges. All other values.keys are still {{}} link to the lesson: https://teamtreehouse.com/library/build-a-simple-dynamic-site-with-nodejs/creating-a-basic-template-engine-in-nodejs/binding-values
var fs = require('fs');
function mergeValues(values, content){
//Cycle over the keys
for(var key in values){
content = content.replace("{{" + key + "}}", values[key]);
}
//return merged content
return content;
}
function view(templateName, values, res){
//Read from the template file
var fileContents = fs.readFileSync('./views/' + templateName + '.html', {encoding: "utf8"});
//Insert values in to the content
fileContents = mergeValues(values, fileContents);
//Write out to the response
res.write(fileContents);
}
module.exports.view = view;
3 Answers
Vitaly Khe
7,160 PointsMaybe i'm a bit late, as i just started this course and it is 2019 now but maybe for someone has same issues that will be useful. Pls make sure your variables in brackets inside of htmls have same names as values object that you emit in profile.js:
profileInstanceContext.emit("end",
{
"username" : profile.name,
"badges" : profile.badges.length,
"javascriptPoints" : profile.points.JavaScript,
"avatarURL": profile.gravatar_url
});
so in htmls you'll need to have exactly the same names inside brackets to get the code right:
{{username}}
{{badges}}
{{javascriptPoints}}
{{avatarURL}}
Erika Suzuki
20,299 PointsHi, you might have spaces in between your variable names in your template. To cover template values with or without spaces. Just replace the loop bit to cover those.
for(let key in values){
// With spaces
content = content.replace(`{{ ${key} }}`, values[key]);
// Without spaces
content = content.replace(`{{${key}}}`, values[key]);
}
Jason Ziegler
Full Stack JavaScript Techdegree Graduate 39,583 PointsConsider the following, console.dir(values); before your for loop to see if the values are all there. Then double check your views to make sure the name in the {{}} matches the key. Then change (var key) to (let key in values). I was having this problem, what eventually solves it was fixing the fact that I was closing the for loop before the call to replace and it was only happened once and for the javascriptPoints value.
Ryan Doran
7,815 PointsRyan Doran
7,815 PointsAbsolutely the problem for me. The instructor doesn't make clear the necessity of matching the text and case - they aren't just filler words. Good catch!