ejs.js
This file
Path
lib/support/ejs.js
53 lines · 1.6 KiB · JavaScript
Last changed by
Hacking haml-js to work with locals and Expressvisionmediabefore adoption46b3e23
History of this fileTake it
Started specs for viewsvisionmediabefore adoptioneaa399f
lines 1–52why, one step back1/* Ejs template parser for CommonJS2 *3 * Copyright (c) 2009, Howard Rauscher4 * Licensed under the MIT License5 *6 * base on:7 * Simple JavaScript Templating (http://ejohn.org/blog/javascript-micro-templating/)8 * John Resig - http://ejohn.org/ - MIT Licensed9 */10 11(function(){12 var cache = {};13 14 var ejs = this.ejs = {};15 16 ejs.parse = function tmpl(str, options) {17 options = options || {};18 options.context = options.context || {};19 options.locals = options.locals || {};20 21 // Figure out if we're getting a template, or if we need to22 // load the template - and be sure to cache the result.23 var fn = cache[str] ||24 25 // Generate a reusable function that will serve as a template26 // generator (and which will be cached).27 new Function("obj",28 "var p=[];" +29 30 // Introduce the data as local variables using with(){}31 "with(obj){p.push('" +32 33 // Convert the template into pure JavaScript34 str35 .replace(/\-%>(\n|\r)/g, "%>")36 .replace(/[\t\b\f]/g, " ")37 .replace(/[\n\r]/g, "\f")38 .split("<%").join("\t")39 .replace(/((^|%>)[^\t]*)'/g, "$1\r")40 .replace(/\t=(.*?)%>/g, "',$1,'")41 .split("\t").join("');")42 .split("%>").join("p.push('")43 .split("\r").join("\\'").replace(/\f+/g, '\\n') +44 "');}return p.join('');");45 46 cache[str] = fn;47 48 // Provide some basic currying to the user49 return fn.call(options.context, options.locals);50 };51})();52 Hacking haml-js to work with locals and Expressvisionmediabefore adoption46b3e23
line 53why, one step back53exports.render = ejs.parse;