Added inherit function

This commit is contained in:
Egil Moeller 2012-03-13 17:42:15 +01:00
parent 6fb0e00f03
commit 3ffed70850
2 changed files with 21 additions and 11 deletions

View File

@ -30,10 +30,18 @@ exports.info = {
file_stack: [],
};
exports.init = function (b, recursive) {
exports._init = function (b, recursive) {
exports.info.buf_stack.push(exports.info.buf);
exports.info.buf = b;
}
exports._exit = function (b, recursive) {
exports.info.file_stack[exports.info.file_stack.length-1].inherit.forEach(function (item) {
exports.require(item.name, item.args);
});
exports.info.buf = exports.info.buf_stack.pop();
}
exports.begin_capture = function() {
exports.info.buf_stack.push(exports.info.buf.concat());
exports.info.buf.splice(0, exports.info.buf.length);
@ -69,23 +77,25 @@ exports.end_block = function () {
exports.begin_block = exports.begin_define_block;
exports.inherit = function (name, args) {
exports.info.file_stack[exports.info.file_stack.length-1].inherit.push({name:name, args:args});
}
exports.require = function (name, args) {
if (args == undefined) args = {};
if (!exports.info)
exports.init(null);
if ((name.indexOf("./") == 0 || name.indexOf("../") == 0) && exports.info.file_stack.length) {
name = path.join(path.dirname(exports.info.file_stack[exports.info.file_stack.length-1]), name);
name = path.join(path.dirname(exports.info.file_stack[exports.info.file_stack.length-1].path), name);
}
var ejspath = require.resolve(name)
args.e = exports;
var template = '<% e.init(buf); %>' + fs.readFileSync(ejspath).toString();
var template = '<% e._init(buf); %>' + fs.readFileSync(ejspath).toString() + '<% e._exit(); %>';
exports.info.file_stack.push(ejspath);
exports.info.buf_stack.push(exports.info.buf);
var res = ejs.render(template, args);
exports.info.buf = exports.info.buf_stack.pop();
exports.info.file_stack.push({path: ejspath, inherit: []});
var res = ejs.render(template, args);
exports.info.file_stack.pop();
if (exports.info.buf)

View File

@ -1,5 +1,5 @@
<% e.begin_define_block("foo"); %>
YY
<% e.end_define_block(); %>
<% e.inherit("./bar.ejs"); %>
<% e.require("./bar.ejs"); %>
<% e.begin_define_block("foo"); %>
YY
<% e.end_define_block(); %>