diff --git a/core/EjsView.js b/core/EjsView.js index 4efeac3..b0c792a 100644 --- a/core/EjsView.js +++ b/core/EjsView.js @@ -65,7 +65,17 @@ EjsView.prototype.render = function(params, context, inner) { body.push("var slots = [];"); body.push("var slot = function(path, context, inner) {"); body.push(" var slot_id = next_ejs_slot_view_id++;"); - body.push(" slots.push([slot_id, path, context, inner]);"); + body.push(" slots.push(['slot', slot_id, path, context, inner]);"); + body.push(" content.push('%%%EJSSLOT%' + slot_id + '%%%');"); + body.push("};"); + body.push("var partial = function(partial_name, params, inner) {"); + body.push(" var slot_id = next_ejs_slot_view_id++;"); + body.push(" slots.push(['partial', slot_id, partial_name, params, inner]);"); + body.push(" content.push('%%%EJSSLOT%' + slot_id + '%%%');"); + body.push("};"); + body.push("var partials = function(partial_name, array_of_params, inner) {"); + body.push(" var slot_id = next_ejs_slot_view_id++;"); + body.push(" slots.push(['partials', slot_id, partial_name, array_of_params, inner]);"); body.push(" content.push('%%%EJSSLOT%' + slot_id + '%%%');"); body.push("};"); @@ -124,18 +134,65 @@ EjsView.prototype.render = function(params, context, inner) { var slots = ejs_view_response[1]; if (slots.length > 0) { - next_ejs_slot_view_id += slots.length; var chain_elements = []; - slots.forEach(function(element) { - chain_elements.push(function (chain_cb) { - var slot_id = element[0]; - BaseApplication.executePath(element[1], element[2], element[3])(function(slot_response) { - response = response.replace("%%%EJSSLOT%" + slot_id + "%%%", slot_response); - chain_cb(); + + if (slots.length > 0) { + next_ejs_slot_view_id += slots.length; + + slots.forEach(function(element) { + chain_elements.push(function (chain_cb) { + var slot_type = element[0]; + var slot_id = element[1]; + + if (slot_type === 'slot') { + BaseApplication.executePath(element[2], element[3], element[4])(function(slot_response) { + response = response.replace("%%%EJSSLOT%" + slot_id + "%%%", slot_response); + chain_cb(); + }); + } else if (slot_type === 'partial') { + var view_name = element[2]; + var params = element[3]; + var inner = element[4]; + + var view = view_manager.getView('partials/' + view_name); + view.render(params, context, inner)(function(slot_response) { + response = response.replace("%%%EJSSLOT%" + slot_id + "%%%", slot_response); + chain_cb(); + }); + } else if (slot_type === 'partials') { + var view_name = element[2]; + var array_of_params = element[3]; + var inner = element[4]; + + var view = view_manager.getView('partials/' + view_name); + + var sub_chain = []; + + var sub_responses = []; + + array_of_params.forEach(function(params) { + sub_chain.push(function(sub_chain_cb) { + view.render(params, context, inner)(function(slot_response) { + sub_responses.push(slot_response); + sub_chain_cb(); + }); + }); + }); + + sub_chain.push(function() { + response = response.replace("%%%EJSSLOT%" + slot_id + "%%%", sub_responses.join('')); + chain_cb(); + }); + + chain.apply(GLOBAL, sub_chain); + } else { + throw new Error('Unknown slot_type ' + slot_type + ' found!'); + } }); }); - }); + } + chain_elements.push(function () { cb(response); }); diff --git a/core/ViewManager.js b/core/ViewManager.js index ca35c4b..f70b814 100644 --- a/core/ViewManager.js +++ b/core/ViewManager.js @@ -80,15 +80,18 @@ ViewManager.prototype.loadViews = function(path, plugin_name) { try { this.debug('loadViews',"loading views for file extension: js"); - child_process.exec("ls " + path + "views/*.js", function(err, stdout, stderr) { + child_process.exec("cd " + path + "views && find . -name '*.js'", function(err, stdout, stderr) { var view_files = []; if (!err) { var files_in_folder = stdout.split("\n"); for (i in files_in_folder) { - if (files_in_folder[i] !== "") { - view_files.push(files_in_folder[i]); + for (i in files_in_folder) { + var file = files_in_folder[i].substr(2); + if (file !== "") { + view_files.push(file); + } } } @@ -96,7 +99,8 @@ ViewManager.prototype.loadViews = function(path, plugin_name) { for (i in view_files) { - require(view_files[i].substr(0, view_files[i].length - 3)); + var view_file_name = view_files[i]; + require(path + 'views/' + view_file_name); } delete self.current_plugin_name; @@ -113,8 +117,8 @@ ViewManager.prototype.loadViews = function(path, plugin_name) { var view_engines_length = this.view_engines.length; for (var i=0; i