views:

64

answers:

2

I have a basic Express server:

// server.js:
var Express = require('express');
app = Express.createServer();
app.configure(function(){
  app.set('views', Path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  app.set('view options');
});
app.get('/', function (request, response) {
  response.render('welcome', {
    locals: {some: 'Locals'}
  });
});

With a basic jade layout:

// views/layout.jade:
!!! 5
html(lang='en')
  head
    title= pageTitle
  body
    h1= pageTitle
    aside(id="sidebar")= sidebarContent
    #content
      #{body}

And a simple page:

# views/welcome.jade:
// How do I pass pageTitle and sidebarContent out to the layout from here?
p
  Welcome to my fine site!

(In Rails, this might be something like content_for or a simple instance variable.)

A: 

Pass it in locals: {some: 'Locals', pageTitle: 'Welcome!'}

drudge
I don't have `locals` from within that template. I'd have to keep a map of `{ template path: page title }` and look up the page title before rendering. There must be a way to specify it from *within* the template itself.
James A. Rosen
+3  A: 

Express does not have a preconceived notion of "blocks" or whatever they call that in in rails, but you can use a combination of helpers() and dynamicHelpers() to achieve something similar http://expressjs.com/guide.html#app-helpers-obj-

Locals passed are available to both the layout and the page view though

tjholowaychuk
So the idea would be to add a dynamic helper that stores `pageTitle` in the request, which the layout can then read out? That sounds like a fine solution. I might even wrap it up as a middleware and publish it!
James A. Rosen