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

Khaled Pendleton
Khaled Pendleton
20,430 Points

Getting {{variableName}} instead of the actual value

Hello I was following along with the video. My code worked perfectly until the very end when it was supposed to output the values for avatarUrl, username, badges and javascriptPoints. My code still just returns the {{}} with the variable names instead of the actual values.

Here is a copy of my renderer.js file:

var fs = require("fs");

function mergeValues(values, content) {
  //Cycle over the keys
  for(var key in values) {
    //Replace all {{ key }} with the value from the values object
    content = content.replace("{{" + key + "}}", values[key]);
  }
  //return merged content
  return content;
}

function view(templateName, values, response) {
  //Read from the template file
  var fileContents = fs.readFileSync('./views/' + templateName + '.html', {encoding: "utf8"});

  //Insert values into content
  fileContents = mergeValues(values, fileContents);
  //Write out content to the response
  response.write(fileContents);
}

module.exports.view = view;

1 Answer

Joel Kraft
STAFF
Joel Kraft
Treehouse Guest Teacher

Hi Khaled,

I launched a fresh workspace from this video, replaced the entire contents of renderer.js with the code you supplied here. It works as expected. Just as an experiment, I launched the app before adding your code, and saw the behavior you describe.

Are you sure your renderer.js has been saved and is correctly being requried by router.js? In other words, is router.js linking to another, older version of renderer.js?

Also, are you using Workspaces or downloading the project files? I have found an error in the downloadable files, and will raise an issue to correct them, but the error wouldn't result in the behavior you describe here.

At this point, I'd recommend deleting the Workspace for this video (if you've created one), and then launching a fresh Workspace, replacing the contents of renderer.js with the code you have here, and seeing if it's working. If not, let us know, but hopefully this will get you back on track.