Skip to content

Commit

Permalink
Merge branch 'partials'
Browse files Browse the repository at this point in the history
  • Loading branch information
DracoBlue committed Sep 26, 2010
2 parents 6f0d73c + d876aad commit 79c92cd
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 22 deletions.
75 changes: 66 additions & 9 deletions core/EjsView.js
Expand Up @@ -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("};");

Expand Down Expand Up @@ -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);
});
Expand Down
30 changes: 17 additions & 13 deletions core/ViewManager.js
Expand Up @@ -80,23 +80,27 @@ 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);
}
}
}

self.current_plugin_name = 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;
Expand All @@ -113,8 +117,8 @@ ViewManager.prototype.loadViews = function(path, plugin_name) {
var view_engines_length = this.view_engines.length;
for (var i=0; i<view_engines_length; i++) {
(function(view_engine_options) {
self.debug('loadViews',"loading views for file extension: " + file_extension);
var file_extension = view_engine_options[0];
self.debug('loadViews',"loading views for file extension: " + file_extension);
var engine = GLOBAL[view_engine_options[1]];

var part_bootstrap_token_name = bootstrap_token_name + '.' + file_extension;
Expand All @@ -123,26 +127,26 @@ ViewManager.prototype.loadViews = function(path, plugin_name) {
var part_bootstrap_token = bootstrap_manager.createMandatoryElement(part_bootstrap_token_name);

try {
child_process.exec("ls " + path + "views/*." + file_extension, function(err, stdout, stderr) {
child_process.exec("cd " + path + "views && find . -name '*." + file_extension + "'", 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]);
var file = files_in_folder[i].substr(2);
if (file !== "") {
view_files.push(file);
}
}
}

self.current_plugin_name = plugin_name;

for (i in view_files) {
var view_name = view_files[i].substr(path.length + "views/".length);
view_name = view_name.substr(0, view_name.length - file_extension.length - 1);

new engine(view_name, view_files[i]);
var view_file_name = view_files[i];
view_name = view_file_name.substr(0, view_file_name.length - file_extension.length - 1);
new engine(view_name, path + 'views/' + view_file_name);
}

delete self.current_plugin_name;
Expand Down

0 comments on commit 79c92cd

Please sign in to comment.