From 92dc3e59d671b8e8249d3d1d15ae6336d34c0822 Mon Sep 17 00:00:00 2001 From: Egil Moeller Date: Wed, 7 Mar 2012 17:36:11 +0100 Subject: [PATCH] First attempt to implement the block system from django templates on top of the npm module ejs. --- src/node/eejs/eejs.js | 84 ++++++++++++++++++++++++++++++++++ src/node/eejs/examples/bar.ejs | 11 +++++ src/node/eejs/examples/foo.ejs | 7 +++ 3 files changed, 102 insertions(+) create mode 100644 src/node/eejs/eejs.js create mode 100644 src/node/eejs/examples/bar.ejs create mode 100644 src/node/eejs/examples/foo.ejs diff --git a/src/node/eejs/eejs.js b/src/node/eejs/eejs.js new file mode 100644 index 000000000..ed6b329bb --- /dev/null +++ b/src/node/eejs/eejs.js @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2011 RedHog (Egil Möller) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Basic usage: + * + * require("./eejs").require("./examples/foo.ejs") + */ + +var ejs = require("ejs"); +var fs = require("fs"); + +exports.init = function (b, recursive) { + if (!exports.info) { + exports.info = { + buf_stack: [], + block_stack: [], + blocks: {}, + level: 1 + } + } + exports.info.buf = b; +} + +exports.begin_capture = function() { + exports.info.buf_stack.push(exports.info.buf.concat()); + exports.info.buf.splice(0, exports.info.buf.length); +} + +exports.end_capture = function () { + var res = exports.info.buf.join(""); + exports.info.buf.splice.apply( + exports.info.buf, + [0, exports.info.buf.length].concat(exports.info.buf_stack.pop())); + return res; +} + +exports.begin_define_block = function (name) { + if (typeof exports.info.blocks[name] == "undefined") + exports.info.blocks[name] = {}; + exports.info.block_stack.push(name); + exports.begin_capture(); +} + +exports.end_define_block = function () { + content = exports.end_capture(); + var name = exports.info.block_stack.pop(); + if (typeof exports.info.blocks[name].content == "undefined") + exports.info.blocks[name].content = content; + return exports.info.blocks[name].content; +} + +exports.end_block = function () { + var res = exports.end_define_block(); + exports.info.buf.push(res); +} + +exports.begin_block = exports.begin_define_block; + +exports.require = function (name, args) { + if (args == undefined) args = {}; + if (exports.info) + exports.info.buf_stack.push(exports.info.buf); + var res = ejs.render( + fs.readFileSync(require.resolve(name)).toString(), + args); + if (exports.info) + exports.info.buf = exports.info.buf_stack.pop(); + if (exports.info.buf) + exports.info.buf.push(res); + return res; +} diff --git a/src/node/eejs/examples/bar.ejs b/src/node/eejs/examples/bar.ejs new file mode 100644 index 000000000..14ed975e7 --- /dev/null +++ b/src/node/eejs/examples/bar.ejs @@ -0,0 +1,11 @@ +<% var e = require("./eejs.js"); e.init(buf); %> + +a +<% e.begin_block("bar"); %> + A + <% e.begin_block("foo"); %> + XX + <% e.end_block(); %> + B +<% e.end_block(); %> +b diff --git a/src/node/eejs/examples/foo.ejs b/src/node/eejs/examples/foo.ejs new file mode 100644 index 000000000..5dcac61c6 --- /dev/null +++ b/src/node/eejs/examples/foo.ejs @@ -0,0 +1,7 @@ +<% var e = require("./eejs.js"); e.init(buf); %> + +<% e.begin_define_block("foo"); %> +YY +<% e.end_define_block(); %> + +<% e.require("./bar.ejs"); %>