Skip to content

Commit

Permalink
added layout control example
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Apr 2, 2011
1 parent f6e9fb1 commit 25bddf3
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/layout-control/app.js
@@ -0,0 +1,35 @@

// Expose modules in ./support for demo purposes
require.paths.unshift(__dirname + '/../../support');

/**
* Module dependencies.
*/

var express = require('../../lib/express');

var app = express.createServer();

app.set('views', __dirname + '/views');

// set default layout, usually "layout"
app.set('view options', { layout: 'layouts/default' });

// Set our default template engine to "jade"
// which prevents the need for extensions
// (although you can still mix and match)
app.set('view engine', 'ejs');

app.get('/', function(req, res){
res.render('pages/default');
});

app.get('/alternate', function(req, res){
// note that we do not explicitly
// state the layout here, the view does,
// although we could do it here as well.
res.render('pages/alternate');
});

app.listen(3000);
console.log('Express app started on port 3000');
6 changes: 6 additions & 0 deletions examples/layout-control/views/layouts/alternate.ejs
@@ -0,0 +1,6 @@
<html>
<body>
<h1>Alternate Layout</h1>
<%- body %>
</body>
</html>
6 changes: 6 additions & 0 deletions examples/layout-control/views/layouts/default.ejs
@@ -0,0 +1,6 @@
<html>
<body>
<h1>Default Layout</h1>
<%- body %>
</body>
</html>
2 changes: 2 additions & 0 deletions examples/layout-control/views/pages/alternate.ejs
@@ -0,0 +1,2 @@
<% locals.layout = 'layouts/alternate' %>
<h1>Page</h1>
1 change: 1 addition & 0 deletions examples/layout-control/views/pages/default.ejs
@@ -0,0 +1 @@
<h1>Page</h1>
2 changes: 2 additions & 0 deletions test/fixtures/layout-switch.jade
@@ -0,0 +1,2 @@
- locals.layout = 'layouts/alternate'
h1 My Page
1 change: 1 addition & 0 deletions test/fixtures/layouts/alternate.jade
@@ -0,0 +1 @@
#alternate= body
13 changes: 13 additions & 0 deletions test/view.test.js
Expand Up @@ -287,6 +287,19 @@ module.exports = {
{ body: '<cool><p>Welcome</p></cool>' });
},

'test #render() view layout control': function(){
var app = create();
app.set('view engine', 'jade');

app.get('/', function(req, res){
res.render('layout-switch');
});

assert.response(app,
{ url: '/' },
{ body: '<div id="alternate"><h1>My Page</h1></div>' });
},

'test #render() "view engine" with periods in dirname': function(){
var app = create();
app.set('view engine', 'jade');
Expand Down

0 comments on commit 25bddf3

Please sign in to comment.