This commit is contained in:
Egil Moeller 2012-03-08 21:01:01 +01:00
parent 92dc3e59d6
commit f6212f452c
1 changed files with 16 additions and 6 deletions

View File

@ -21,6 +21,7 @@
var ejs = require("ejs"); var ejs = require("ejs");
var fs = require("fs"); var fs = require("fs");
var path = require("path");
exports.init = function (b, recursive) { exports.init = function (b, recursive) {
if (!exports.info) { if (!exports.info) {
@ -28,7 +29,7 @@ exports.init = function (b, recursive) {
buf_stack: [], buf_stack: [],
block_stack: [], block_stack: [],
blocks: {}, blocks: {},
level: 1 filestack: [],
} }
} }
exports.info.buf = b; exports.info.buf = b;
@ -71,13 +72,22 @@ exports.begin_block = exports.begin_define_block;
exports.require = function (name, args) { exports.require = function (name, args) {
if (args == undefined) args = {}; if (args == undefined) args = {};
if (exports.info) if (!exports.info)
exports.init(null);
if ((name.indexOf("./") == 0 || name.indexOf("../") == 0) && exports.info.file_stack) {
name = path.join(exports.info.file_stack[exports.info.file_stack.length-1], name);
}
var ejspath = require.resolve(name)
exports.info.file_stack.push(ejpath);
exports.info.buf_stack.push(exports.info.buf); exports.info.buf_stack.push(exports.info.buf);
var res = ejs.render( var res = ejs.render(
fs.readFileSync(require.resolve(name)).toString(), fs.readFileSync(ejspath).toString(),
args); args);
if (exports.info)
exports.info.buf = exports.info.buf_stack.pop(); exports.info.buf = exports.info.buf_stack.pop();
exports.info.file_stack.pop();
if (exports.info.buf) if (exports.info.buf)
exports.info.buf.push(res); exports.info.buf.push(res);
return res; return res;