Skip to content

Commit

Permalink
added layout to views
Browse files Browse the repository at this point in the history
now under views will be a new directory named 'layout'.
in it should be a file called default.html.ejs
this file will be rendered after the controller action
view was rendered. the content of the controller action view
will be replaced with <%= yield %> in layout
  • Loading branch information
scasei committed Sep 30, 2010
1 parent bc08a93 commit cb37801
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 86 deletions.
2 changes: 1 addition & 1 deletion geddy-core/lib/app.js
Expand Up @@ -133,7 +133,7 @@ var App = function (initData) {

// The route matches -- we have a winner!
if (params) {
log.debug('Routed to ' + params.controller + ' controller, ' + params.action + ' action');
log.debug('Routed to ' + params.controller + ' controller, ' + params.action + ' action, ' + params.format + ' format');

// Set up the cookies for this request so we can do the session thing
cook = new cookies.CookieCollection(req);
Expand Down
20 changes: 16 additions & 4 deletions geddy-core/lib/controller.js
Expand Up @@ -56,6 +56,9 @@ var Controller = function (obj) {
// The template root to look in for partials when rendering templates
// Gets created programmatically based on controller name -- see renderTemplate
this.templateRoot = undefined;
// The template layout directory to look in when rendering templates
// Gets created programmatically based on controller name -- see renderTemplate
this.templateLayout = 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 @@ -337,13 +340,17 @@ Controller.prototype = new function () {
}
};

this.renderTemplate = function (data) {
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;
this.templateRoot = this.templateRoot ||
'app/views/' + geddy.inflections[this.name].filename.plural;

// Calculate the templateLayout if not set
this.templateLayout = this.templateLayout ||
'app/views/layout';

var templater = new Templater();
var content = '';

Expand All @@ -356,7 +363,12 @@ Controller.prototype = new function () {
_this.formatContentAndFinish(content);
});

templater.render(data, [this.templateRoot], this.params.action);
templater.render(data
,{
layout:this.templateLayout
,content:this.templateRoot
}
,this.params.action);
};

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

var cmds = [
'mkdir -p ./app/views/' + names.filename.plural
, 'mkdir -p ./app/views/layout'
, 'cp -u ~/.node_libraries/geddy-core/scripts/gen/views/layout.html.ejs ' +
'./app/views/layout/default.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
9 changes: 2 additions & 7 deletions geddy-core/scripts/gen/views/add.html.ejs
@@ -1,13 +1,8 @@
<html>
<head>
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>

<h3>Params</h3>
<ul>
<% for (var p in params) { %>
<li><%= p + ': ' + params[p]; %></li>
<% } %>
</ul>
</body>
</html>

38 changes: 17 additions & 21 deletions geddy-core/scripts/gen/views/add_scaffold.html.ejs
@@ -1,6 +1,18 @@
<@ params.formAction = '/<%= names.filename.plural %>'; @>
<html>
<head>

<@ params.formAction = '/<%= names.filename.plural %>'; @>
<h3>
New <%= names.constructor.singular %>
</h3>
<@ if (params.errors) { @>
<p>
<div>Errors:</div>
<@ for (var p in params.errors) { @>
<div><@= p @>: <@= params.errors[p] @></div>
<@ } @>
</p>
<@ } @>
<@= partial('_form', {params: params}); @>

<script type="text/javascript" src="/js/util/date.js"></script>
<script type="text/javascript" src="/js/util/form.js"></script>
<script type="text/javascript" src="/js/util/meta.js"></script>
Expand Down Expand Up @@ -30,23 +42,7 @@
}
};
</script>
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>
<h3>
New <%= names.constructor.singular %>
</h3>
<@ if (params.errors) { @>
<p>
<div>Errors:</div>
<@ for (var p in params.errors) { @>
<div><@= p @>: <@= params.errors[p] @></div>
<@ } @>
</p>
<@ } @>
<@= partial('_form', {params: params}); @>
</body>
</html>
</script>



7 changes: 0 additions & 7 deletions geddy-core/scripts/gen/views/edit.html.ejs
@@ -1,14 +1,7 @@
<html>
<head>
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>
<h3>Params</h3>
<ul>
<% for (var p in params) { %>
<li><%= p + ': ' + params[p]; %></li>
<% } %>
</ul>
</body>
</html>

39 changes: 18 additions & 21 deletions geddy-core/scripts/gen/views/edit_scaffold.html.ejs
@@ -1,6 +1,19 @@
<@ params.formAction = '/<%= names.filename.plural %>/' + params.id + '?_method=put'; @>
<html>
<head>

<@ params.formAction = '/<%= names.filename.plural %>/' + params.id + '?_method=put'; @>

<h3>
Edit <%= names.constructor.singular %> <@= params.id @>
</h3>
<@ if (params.errors) { @>
<p>
<div>Errors:</div>
<@ for (var p in params.errors) { @>
<div><@= p @>: <@= params.errors[p] @></div>
<@ } @>
</p>
<@ } @>
<@= partial('_form', {params: params}); @>

<script type="text/javascript" src="/js/util/date.js"></script>
<script type="text/javascript" src="/js/util/form.js"></script>
<script type="text/javascript" src="/js/util/meta.js"></script>
Expand Down Expand Up @@ -30,23 +43,7 @@
}
};
</script>
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>
<h3>
Edit <%= names.constructor.singular %> <@= params.id @>
</h3>
<@ if (params.errors) { @>
<p>
<div>Errors:</div>
<@ for (var p in params.errors) { @>
<div><@= p @>: <@= params.errors[p] @></div>
<@ } @>
</p>
<@ } @>
<@= partial('_form', {params: params}); @>
</body>
</html>
</script>



9 changes: 2 additions & 7 deletions geddy-core/scripts/gen/views/index.html.ejs
@@ -1,13 +1,8 @@
<html>
<head>
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>

<h3>Params</h3>
<ul>
<% for (var p in params) { %>
<li><%= p + ': ' + params[p]; %></li>
<% } %>
</ul>
</body>
</html>

9 changes: 2 additions & 7 deletions geddy-core/scripts/gen/views/index_scaffold.html.ejs
@@ -1,9 +1,5 @@
<@ var items = params.items; @>
<html>
<head>
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>

<p>
<a href="/<%= names.filename.plural %>/add">Create new item</a>
<p>
Expand Down Expand Up @@ -48,6 +44,5 @@
<@ } @>
</table>
<@ } @>
</body>
</html>


8 changes: 8 additions & 0 deletions geddy-core/scripts/gen/views/layout.html.ejs
@@ -0,0 +1,8 @@
<html>
<head>
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>
<%= yield %>
</body>
</html>
9 changes: 2 additions & 7 deletions geddy-core/scripts/gen/views/show.html.ejs
@@ -1,13 +1,8 @@
<html>
<head>
<link type="text/css" rel="stylesheet" href="/css/master.css"/>
</head>
<body>

<h3>Params</h3>
<ul>
<% for (var p in params) { %>
<li><%= p + ': ' + params[p]; %></li>
<% } %>
</ul>
</body>
</html>

32 changes: 28 additions & 4 deletions geddy-template/lib/adapters/ejs/index.js
Expand Up @@ -39,10 +39,34 @@ Templater.prototype.templateRoot = undefined;

// Override the TempaterBase render method
Templater.prototype.render = function (data, paths, filename) {
// Set the base path to look for template partials
this.templateRoot = paths[0];
// Start rendering from the partials root
this.partial(filename, data);

if (paths.layout) {

this.templateRoot = paths.layout;

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

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

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

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


Expand Down

0 comments on commit cb37801

Please sign in to comment.