Skip to content

Commit

Permalink
Some tweaks to scasei's patch for layouts.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Oct 4, 2010
1 parent fdb120c commit eefb7fc
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 65 deletions.
28 changes: 14 additions & 14 deletions geddy-core/lib/controller.js
Expand Up @@ -55,10 +55,10 @@ var Controller = function (obj) {
this.contentType = '';
// The template root to look in for partials when rendering templates
// Gets created programmatically based on controller name -- see renderTemplate
this.templateRoot = undefined;
this.template = undefined;
// The template layout directory to look in when rendering templates
// Gets created programmatically based on controller name -- see renderTemplate
this.templateLayout = undefined;
this.layout = undefined;
// This will be used for 'before' actions for plugins
this.beforeFilters = [];
// This will be used for 'after' actions for plugins
Expand Down Expand Up @@ -343,13 +343,13 @@ Controller.prototype = new function () {
this.renderTemplate = function (data) {
var _this = this;

// Calculate the templateRoot if not set
this.templateRoot = this.templateRoot ||
'app/views/' + geddy.inflections[this.name].filename.plural;
// Calculate the template if not set
this.template = this.template ||
'app/views/' + geddy.inflections[this.name].filename.plural + '/' + this.params.action;

// Calculate the templateLayout if not set
this.templateLayout = this.templateLayout ||
'app/views/layout';
// Calculate the layout if not set
this.layout = this.layout ||
'app/views/layouts/' + geddy.inflections[this.name].filename.plural;

var templater = new Templater();
var content = '';
Expand All @@ -363,12 +363,12 @@ Controller.prototype = new function () {
_this.formatContentAndFinish(content);
});

templater.render(data
,{
layout:this.templateLayout
,content:this.templateRoot
}
,this.params.action);
templater.render(data, {
layout: this.layout
, template: this.template
, controller: this.name
, action: this.params.action
});
};

}();
Expand Down
4 changes: 2 additions & 2 deletions geddy-core/scripts/Jakefile.js
Expand Up @@ -175,9 +175,9 @@ task('resource', [], function (nameParam) {

var cmds = [
'mkdir -p ./app/views/' + names.filename.plural
, 'mkdir -p ./app/views/layout'
, 'mkdir -p ./app/views/layouts'
, 'cp -u ~/.node_libraries/geddy-core/scripts/gen/views/layout.html.ejs ' +
'./app/views/layout/default.html.ejs'
'./app/views/layouts/application.html.ejs'
, 'cp ~/.node_libraries/geddy-core/scripts/gen/views/add.html.ejs ' +
'./app/views/' + names.filename.plural + '/'
, 'cp ~/.node_libraries/geddy-core/scripts/gen/views/edit.html.ejs ' +
Expand Down
4 changes: 2 additions & 2 deletions geddy-core/scripts/gen/views/layout.html.ejs
Expand Up @@ -3,6 +3,6 @@
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>
<%= yield %>
<%= yield(); %>
</body>
</html>
</html>
108 changes: 61 additions & 47 deletions geddy-template/lib/adapters/ejs/index.js
Expand Up @@ -26,67 +26,74 @@ var sys = require('sys'),
* EJS templater constructor
* @contstructor
*/
var Templater = function () {};
var Templater = function () {
this.currentPartialId = 0;
this.baseTemplateNode = undefined;
this.templateRoot = undefined;
this.isLayout = false;
};

// Inherit from TemplaterBase
Templater.prototype = new TemplaterBase();

Templater.prototype.currentPartialId = 0;
// Override the TempaterBase render method
Templater.prototype.render = function (data, config) {

Templater.prototype.baseTemplateNode = undefined;
if (config.layout) {

Templater.prototype.templateRoot = undefined;
this.isLayout = true;
this.templateRoot = getDirname(config.layout);

// Override the TempaterBase render method
Templater.prototype.render = function (data, paths, filename) {

if (paths.layout) {

this.templateRoot = paths.layout;

var _this = this;
var _this = this;
var templaterContent = new Templater();
var contentPartial = '';

templaterContent.addListener('data', function (d) {
// Buffer for now, but could stream
contentPartial += d;
contentPartial += d;
});

templaterContent.addListener('end', function () {
data['yield'] = contentPartial;
_this.partial('default', data);
});

templaterContent.render(data
,{content:paths.content}
,filename);
}
data.yield = function () { return contentPartial; };
_this.partial(getFilename(config.layout), data);
});

templaterContent.render(data, {template: config.template});
}

else {
// Set the base path to look for template partials
this.templateRoot = paths.content || paths[0];
this.partial(filename, data);
this.templateRoot = getDirname(config.template);
filename = getFilename(config.template);
this.partial(filename, data);
}
};

var getFilename = function (path) {
return path.split('/').pop();
};

var getDirname = function (path) {
var arr = path.split('/');
arr.pop();
return arr.join('/');
};

var getTemplateUrl = function (templateRoot, partialUrl, parentNode, isLayout) {
var key
, templateUrl
, dirs = []
, dir
, err;

var getTemplateUrl = function (templateRoot, partialUrl, parentNode) {
var key,
templateUrl,
dirs = [],
dir;

// If this is a sub-template, try in the same directory as the the parent
if (parentNode) {
dirs.push(parentNode.dirname);
}
// Or fall back to the templateRoot

// Or look in the specified the templateRoot
dirs.push(templateRoot);

// Or fall back to the fallback of the root of the views directory
dirs.push('app/views');

// Look through the directory list until you find a registered
// template path -- these are registered during app init so we're
// not touching the filesystem every time to look for partials
Expand All @@ -98,26 +105,33 @@ var getTemplateUrl = function (templateRoot, partialUrl, parentNode) {
break;
}
}
// Bail out if we can't find a template

// No template
if (!templateUrl) {
var e = new errors.InternalServerError('Partial template "' +
partialUrl + '" not found in ' + dirs.join(", "));
throw e;
// If it's a layout, use the default one for the app
if (isLayout) {
templateUrl = 'app/views/layouts/application.html.ejs';
}
// Bail out if a normal content template
else {
err = new errors.InternalServerError('Partial template "' +
partialUrl + '" not found in ' + dirs.join(", "));
throw err;
}
}

return templateUrl;
};

Templater.prototype.partial = function (partialUrl, renderContext, parentNode) {

var _this = this,
node,
partialId = this.currentPartialId,
isBaseNode = !this.baseTemplateNode,
templateUrl;
templateUrl = getTemplateUrl(this.templateRoot, partialUrl, parentNode);

templateUrl = getTemplateUrl(this.templateRoot, partialUrl, parentNode, this.isLayout);

// Create the current node, with a reference to its parent, if any
node = new TemplateNode(partialId, templateUrl, renderContext, parentNode);
Expand All @@ -127,12 +141,12 @@ Templater.prototype.partial = function (partialUrl, renderContext, parentNode) {
renderContext.partial = function (partUrl, ctxt) {
return _this.partial.call(_this, partUrl, ctxt, node);
};

// If there is a parent, add this node as its child
if (parentNode) {
parentNode.childNodes[partialId] = node;
}

// If this is the base node (i.e., there's no baseTemplateNode yet),
// give this node the finishRoot method that actually renders the final,
// completed content for the entire template
Expand All @@ -147,10 +161,10 @@ Templater.prototype.partial = function (partialUrl, renderContext, parentNode) {
// Kick off the hierarchical async loading process
node.loadTemplate();
}

// Increment the current partial id for the next call
this.currentPartialId++;

// Return the placeholder text to represent this template -- it gets
// replaced in the callback from the async load of the actual content
return '###partial###' + partialId;
Expand Down

0 comments on commit eefb7fc

Please sign in to comment.