Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass content from a template to a layout in Express?

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.)

like image 709
James A. Rosen Avatar asked Aug 30 '10 14:08

James A. Rosen


People also ask

What is the use of template engine in Express?

A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client.

What is Jade in Express?

Jade is a template engine for node. js and the default rendering engine for the Express web framework. It is a new, simplified language that compiles into HTML and is extremely useful for web developers. Jade is designed primarily for server-side templating in node.


1 Answers

Using the tip above about dynamicHelpers, and the magic of closures, I found a fairly elegant solution that works without involving the request object. The trick is to wrap the page title variable in a closure that provides a get() and set() function around it, and make that wrapper object the result of the page_title dynamic helper.

Create a property.js:

exports.create = function () {
    var value = null;
    return {
        get: function () {
           return value;
        },
        set: function (new_value) {
           value = new_value;
        }
    };
}

So calling create() returns an object with a get() and set() method on it, that get and set the closure variable.

Then, in your app's setup code:

    var property = require("./property.js");
    app.dynamicHelpers ({
        page_title: function () {
         return property.create ();
        }
    });

Since the dynamic helper's value is the result of calling its function, in your view and template, the page_title variable will be the wrapper object with the get() and set() functions.

In your view, you can then say:

- page_title.set ("my specific page title");

And in your layout:

title= page_title.get()

To simplify this a bit further, adding this to property.js:

exports.creator = function () {
    return function () {
        return exports.create();
    };
}

Lets you simplify the dynamic helpers declaration block to this:

        var property = require("./property.js");
        app.dynamicHelpers ({
            page_title: property.creator()
        });
like image 146
Jake Avatar answered Oct 24 '22 23:10

Jake