comprise

Add layout and partial support to consolidate.js!

npm install comprise
1 downloads in the last week
4 downloads in the last month

Comprise add layout and partial support to consolidate.js!

Comprise add layout and partial support to the great consolidate.js template engine.

  • Add layout support to your templates, incl.
    • Default templates
    • Template hierarchies
    • "no template" option.
  • Partials could use the origin and optional a new variable scope.

Tested with:

Doesn't work yet with:

  • haml -- ! layout('default') and != partial('partial1') does not work. Support welcome!
  • mustache -- {{{ layout 'default' }}} and {{ partial("partial1") }} does not work. Support welcome!

API

var Comprise = require('comprise').Comprise;

var comprise = new Comprise({
    engine: 'jade',
    layout: 'default',
    templateDir: __dirname + '/views'
});

comprise.render('your jade file', { user: user }, function(err, result) {
    if (err) {
        console.error(err);
    } else {
        console.log(result);
    }
});

Available options:

  • engine - See consolidate.js engine list
  • layout - Optional default layout
  • extension - file name extension, default is simular to engine
  • templateDir - Required
  • layoutDir - Optional, default is $templateDir/_layout
  • partialDir - Optional, default is $templateDir/_partials

express framework integration

var comprise = require('comprise');

app.engine('jade', comprise.express({
    engine: 'jade',
    layout: 'default'
}));
app.set('view engine', 'jade');

Available options see API section above.

templateDir was automatically set based on the express setting views (default ./views).

Example

Just render content.jade, but use the common default.jade template which extends the layout.jade.

content.jade template:

h1 This content use the standard layout!

div
    != partial('partial1')
div
    != partial('partial2.jade')

layout.jade layout:

html
    head
        title comprise
    body
        != content()

default.jade included layout:

= layout('layout')

header
    nav navigation code

!= content()

How it works

Comprise provides a simple wrapper around consolidate.js and add four small functions to the local template variables:

  • layout(layout)
  • nolayout()
  • content()
  • partial(partialTemplate, partialVariables)

Installation

npm install comprise --production

Run the unit tests

npm install --save-dev
npm test
npm loves you