Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js Binding Values

Ginger Williams
Ginger Williams
6,409 Points

Still 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
Vitaly Khe
7,160 Points

Maybe 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}}
Ryan Doran
Ryan Doran
7,815 Points

Absolutely 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!

Erika Suzuki
Erika Suzuki
20,299 Points

Hi, 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jason Ziegler
Full Stack JavaScript Techdegree Student 36,756 Points

Consider 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.