From 0d23044ee0262af28cfa23438d1b8558b358e2de Mon Sep 17 00:00:00 2001 From: Ishai Date: Tue, 24 Nov 2015 11:26:20 +0200 Subject: [PATCH 1/2] super simple block syntax --- index.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 230acfa..548c7c7 100644 --- a/index.js +++ b/index.js @@ -60,6 +60,52 @@ var ejs = require('ejs') * */ +/* + * Override EJS renderFile to allow super simple block structure + * + */ + + var BLOCK_REGEX = /<%\s*block\s+([$a-zA-Z_][a-zA-Z_0-9]*)\s*%>((?:.|\s)*?)<%\s*\/block\s*%>/ig; + var cache; + var fs = require('fs'); +ejs.renderFile = function(path, options, fn){ + var key = path + ':string'; + + if ('function' == typeof options) { + fn = options, options = {}; + } + + options.filename = path; + + try { + var str = options.cache + ? cache[key] || (cache[key] = fs.readFileSync(path, 'utf8')) + : fs.readFileSync(path, 'utf8'); + + // New code. + // Uses Regex to catch all blocks, render them, save them in object, and clean them from html + var blocks = {}; + str = str.replace(BLOCK_REGEX,function(_,blockName,blockContent){ + blocks[blockName] = ejs.render(blockContent,options); + return ''; + }); + // return html and generated blocks + fn(null, ejs.render(str, options),blocks); + } catch (err) { + fn(err); + } +}; + + /** + * Render an EJS file at the given `path` and callback `fn(err, str)`. + * + * @param {String} path + * @param {Object|Function} options or callback + * @param {Function} fn + * @api public + */ + + var renderFile = module.exports = function(file, options, fn){ // Express used to set options.locals for us, but now we do it ourselves @@ -82,12 +128,17 @@ var renderFile = module.exports = function(file, options, fn){ options.locals.layout = layout.bind(options); options.locals.partial = partial.bind(options); - ejs.renderFile(file, options, function(err, html) { + ejs.renderFile(file, options, function(err, html,blocks) { if (err) { return fn(err,html); } - + // put rendered blocks in locals + if(blocks){ + for(var blockName in blocks){ + options.block(blockName,blocks[blockName]); + } + } var layout = options.locals._layoutFile; // for backward-compatibility, allow options to From 8eb63d9c989bae89737e5ee67fd89f4f08f2e6ec Mon Sep 17 00:00:00 2001 From: Ishai Date: Wed, 30 May 2018 15:55:22 +0300 Subject: [PATCH 2/2] fix for production --- index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.js b/index.js index 548c7c7..b5350e7 100644 --- a/index.js +++ b/index.js @@ -85,10 +85,15 @@ ejs.renderFile = function(path, options, fn){ // New code. // Uses Regex to catch all blocks, render them, save them in object, and clean them from html var blocks = {}; + var filename = options.filename; str = str.replace(BLOCK_REGEX,function(_,blockName,blockContent){ + // use different cache key + options.filename = filename + "?block_" + blockName; blocks[blockName] = ejs.render(blockContent,options); return ''; }); + // restore cache key + options.filename = filename; // return html and generated blocks fn(null, ejs.render(str, options),blocks); } catch (err) {