added layout control example

This commit is contained in:
Tj Holowaychuk 2011-04-01 17:39:18 -07:00
parent f6e9fb13f8
commit 25bddf3fb5
8 changed files with 66 additions and 0 deletions

View File

@ -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');

View File

@ -0,0 +1,6 @@
<html>
<body>
<h1>Alternate Layout</h1>
<%- body %>
</body>
</html>

View File

@ -0,0 +1,6 @@
<html>
<body>
<h1>Default Layout</h1>
<%- body %>
</body>
</html>

View File

@ -0,0 +1,2 @@
<% locals.layout = 'layouts/alternate' %>
<h1>Page</h1>

View File

@ -0,0 +1 @@
<h1>Page</h1>

2
test/fixtures/layout-switch.jade vendored Normal file
View File

@ -0,0 +1,2 @@
- locals.layout = 'layouts/alternate'
h1 My Page

1
test/fixtures/layouts/alternate.jade vendored Normal file
View File

@ -0,0 +1 @@
#alternate= body

View File

@ -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');