Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Started implementation

  • Loading branch information...
commit 23dc63138969b58fe421d83a2c3c5bbe7bf8af36 1 parent b0b0973
@tj tj authored
Showing with 116 additions and 1 deletion.
  1. +1 −1  Makefile
  2. +89 −0 lib/ejs.js
  3. +26 −0 test/ejs.test.js
View
2  Makefile
@@ -1,5 +1,5 @@
test:
- @./support/expresso/bin/expresso
+ @./support/expresso/bin/expresso -I lib test/*.test.js
.PHONY: test
View
89 lib/ejs.js
@@ -0,0 +1,89 @@
+
+/*!
+ * EJS
+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var sys = require('sys');
+
+/**
+ * Intermediate js cache.
+ *
+ * @type Object
+ */
+
+var cache = {};
+
+/**
+ * Parse the given `str` of ejs, returning the function body.
+ *
+ * @param {String} str
+ * @return {String}
+ * @api public
+ */
+
+var parse = exports.parse = function(str){
+ return 'var buf = [];\n'
+ + "with (locals) {\nbuf.push('"
+ + String(str)
+ .replace(/[\r\t\n]/g, " ")
+ .split("<%").join("\t")
+ .replace(/((^|%>)[^\t]*)'/g, "$1\r")
+ .split("'").join("\\'")
+ .replace(/\t=(.*?)%>/g, "',$1,'")
+ .split("\t").join("');")
+ .split("%>").join("buf.push('")
+ .split("\r").join("\\'")
+ + "');\n}\nreturn buf.join('');";
+};
+
+/**
+ * Compile the given `str` of ejs into a `Function`.
+ *
+ * @param {String} str
+ * @param {Object} options
+ * @return {Function}
+ * @api public
+ */
+
+var compile = exports.compile = function(str, options){
+ if (options.debug) sys.puts(parse(str));
+ return new Function('locals', parse(str));
+};
+
+/**
+ * Render the given `str` of ejs.
+ *
+ * Options:
+ *
+ * - `locals` Local variables object
+ * - `cache` Compiled functions are cached, requires `filename`
+ * - `filename` Used by `cache` to key caches
+ * - `context|scope` Function execution context
+ * - `debug` Output generated function body
+ *
+ * @param {String} str
+ * @param {Object} options
+ * @return {String}
+ * @api public
+ */
+
+exports.render = function(str, options){
+ var fn,
+ options = options || {};
+ if (options.cache) {
+ if (options.filename) {
+ fn = cache[options.filename] = compile(str, options);
+ } else {
+ throw new Error('"cache" option requires "filename".');
+ }
+ } else {
+ fn = compile(str, options);
+ }
+ return fn.call(options.context, options.locals || {});
+};
View
26 test/ejs.test.js
@@ -0,0 +1,26 @@
+
+/**
+ * Module dependencies.
+ */
+
+var ejs = require('ejs');
+
+module.exports = {
+ 'test html': function(assert){
+ assert.equal('<p>yay</p>', ejs.render('<p>yay</p>'));
+ },
+
+ 'test buffered code': function(assert){
+ var html = '<p>tj</p>',
+ str = '<p><%= name %></p>',
+ locals = { name: 'tj' };
+ assert.equal(html, ejs.render(str, { locals: locals }));
+ },
+
+ 'test unbuffered code': function(assert){
+ var html = '<p>tj</p>',
+ str = '<% if (name) { %><p><%= name %></p><% } %>',
+ locals = { name: 'tj' };
+ assert.equal(html, ejs.render(str, { locals: locals }));
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.