diff --git a/lib/textlates.js b/lib/textlates.js index 5bd8581..84af700 100644 --- a/lib/textlates.js +++ b/lib/textlates.js @@ -1,24 +1,20 @@ var fs = require('fs'); -function render(file, data, cb){ - var found = {}; +function render(template, vars, cb){ + fs.readFile(template, 'utf8', function(err, template){ + if(err) return cb(err); - fs.readFile(file, 'utf8', function(err, file){ - if(err) - return cb(err); + var replacementRegex + , identifier + ; - var values = file.match(/#{[^}\r\n]*}/g); - - for(var value in values){ - value = values[value].substring(2, values[value].length - 1);; - - if(!found[value]){ - file = file.replace('#{' + value + '}', data[value]); - found[value] = true; - } + for (identifier in vars) { + // Create dynamic regex that will find and replace all instances of the identifier + replacementRegex = new RegExp('#{' + identifier + '}', 'g'); + template = template.replace(replacementRegex, vars[identifier]); } - return cb(null, file); + return cb(null, template); }); } diff --git a/test/test.txt b/test/test.txt index 3a6a791..cbbd261 100644 --- a/test/test.txt +++ b/test/test.txt @@ -2,7 +2,7 @@ Hello #{name}. A standard greeting may look like this: -#{greeting} +#{greeting}, #{name} How does that look to you?