using partials with express in node.js

Nic Meiring picture Nic Meiring · Apr 19, 2012 · Viewed 12.5k times · Source

I am trying to render partials using node.js. Here is my code.

app.js:

  var express = require('express')
  , routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

var products = require('./products.js');

app.get('/products', function(req, res) {
    res.render('products/index', {locals: {
               products: products.all
               }
               });
});

app.listen(3000);

When I go to localhost:3000/products it should render index.jade which is in the products folder which is in the views folder.Above I set the views directory using app.set('views', __dirname + '/views');

index.jade:

h1 Products:
#products!= partial('partials/product', {collection: products})

This should render the partial equivalent to (partials/product.jade) because jade is my view engine.

I am getting an error back saying "partial is not defined"

Any help would be great. thanks

UPDATE:

That solved my partial error thank you. I reinstalled 2.5.9.

Answer

Jonathan Lonowski picture Jonathan Lonowski · Apr 19, 2012

Check what version of Express JS you have installed -- you may have the 3.0 alpha:

$ npm ls
...
└─┬ [email protected]
...

If you're interested in trying the alpha, be sure to checkout the documentation on Migrating from 2.x to 3.x. In it, you'll notice that res.partial() and partial() (within templates) have been removed -- as described under "View system changes:"

By removing the concept of a "layout" & partials in Express 3.x template engines will have greater control over file I/O. This means integration with template engines much easier, and greatly simplify the view system's internals.

You can see an example of the intent in the linked article, Use Jade blocks, not layouts.

If you're not interested, then just make sure you have 2.x installed.

$ npm install [email protected]

Or via package.json:

{
    ...
    "dependencies": {
        ...
        "express": "2.x"
    }
}