Error: failed to locate view "layout"

2,132 views
Skip to first unread message

Geuis

unread,
Mar 23, 2011, 4:50:24 AM3/23/11
to Express
Hey, I'm trying to get Express setup for the first time tonight. I've
read through all of the documentation a couple of times and honestly,
I'm just finding it confusing and lacking in important details in lots
of places. I intend on using the jQuery templating system, so I
installed that via npm.

I have a directory for my project setup like this:

/app.js
/pages/index.html

This is the code I'm working with at the moment:

var express = require('express'),
jqtpl = require('jqtpl');

//create server
var app = express.createServer();

//configure templating engine
app.set('view engine', 'html');
app.register('.html', require('jqtpl') );
app.set('views', __dirname+'/pages');

app.get('/', function(req, res){

res.render('index');

});

app.listen(8080);

I read that CWD/views is the default views directory, but even when I
renamed 'pages' to 'views' in the file system and changed
app.set('views'... to app.set('views', 'views') I still get the same
error in the subject.

This is the full error that node throws:
Error: failed to locate view "layout"
at ServerResponse.render (/usr/local/Cellar/node/0.4.3/lib/
node/.npm/express/2.0.0/package/lib/view.js:285:11)
at ServerResponse.render (/usr/local/Cellar/node/0.4.3/lib/
node/.npm/express/2.0.0/package/lib/view.js:329:12)
at Object.<anonymous> (/Users/geuis/Dropbox/logger/app.js:26:13)
at param (/usr/local/Cellar/node/0.4.3/lib/node/.npm/connect/1.1.3/
package/lib/middleware/router.js:147:21)
at pass (/usr/local/Cellar/node/0.4.3/lib/node/.npm/connect/1.1.3/
package/lib/middleware/router.js:163:10)
at Object.router [as handle] (/usr/local/Cellar/node/0.4.3/lib/
node/.npm/connect/1.1.3/package/lib/middleware/router.js:169:6)
at next (/usr/local/Cellar/node/0.4.3/lib/node/.npm/connect/1.1.3/
package/lib/http.js:204:15)
at Object.handle (/usr/local/Cellar/node/0.4.3/lib/node/.npm/
express/2.0.0/package/lib/http.js:73:5)
at next (/usr/local/Cellar/node/0.4.3/lib/node/.npm/connect/1.1.3/
package/lib/http.js:204:15)
at HTTPServer.handle (/usr/local/Cellar/node/0.4.3/lib/node/.npm/
connect/1.1.3/package/lib/http.js:217:3)

What's the right way to set up a new express project?

Joshua Cohen

unread,
Mar 23, 2011, 9:33:10 AM3/23/11
to expre...@googlegroups.com
By default Express will render a common layout page as a wrapper around each individual page (so you can easily reuse the shell containing common markup on every page for example). If you don't want a layout you can do one of the following:

1) Call app.set("view options", { layout: false }) to tell Express not to render a layout on a global basis.
2) When rendering your view pass a layout parameter in the options: app.render("index", { layout: false }); to override the layout settings on an individual basis
3) Add layout.html to your views directory. Layout will have access to a special "body" property that contains the rendered output of your template. So you'd want something like:

<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
{{html body}}
</body>
</html>

(If I'm remembering jqtpl correctly, anyway).

Cheers,

Joshua


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.


vision media [ Tj Holowaychuk ]

unread,
Mar 23, 2011, 11:37:30 AM3/23/11
to expre...@googlegroups.com
also although "views" defaults to CWD/views, it's bad practice to leave things relative to the CWD, so you will most likely want to do app.set("views", __dirname + '/views');
--
Tj Holowaychuk
Vision Media
President & Creative Lead
Reply all
Reply to author
Forward
0 new messages