Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

using partials with express in node.js

using partials with express in node.js

Problem

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.

Problem courtesy of: Nic Meiring

Solution

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:"

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"
    }
}
Solution courtesy of: Jonathan Lonowski

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

using partials with express in node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×