Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Getting Started In Node

chrisdickinson edited this page · 4 revisions

Getting Started / In Node

Welcome, node.js-faring djangonauts (and other interested parties!) I'm pleased to present the two steps necessary to install plate and setup a loading system!

Step 1: Install it

$ npm install plate

Done. Take a break, have some coffee or beer. It's obvious that you must be exhausted by now.

Step 2: Configure it

Open a file in your project -- say, lib/template.js.

// lib/template.js
var plate = require('plate')
  , Loader = require('plate/lib/plugins/loaders/filesystem').Loader
  , path = require('path')

var plugin;
module.exports = plugin = new Loader([
    path.resolve(path.join(__dirname, '../templates'))
]).getPlugin()

Loader takes an array of paths to search when looking for a template. This example will look for templates in <yourproject>/templates. We make sure to export the plugin as well, so in our controller, we can do something like this:

// lib/controller.js

// NOTE: this is (sort of) assuming we're using express / mongoose.
// but neither of those two libraries are *actually* required.
// they just make for a nice "real world"-y example.

var template = require('./template')
exports.list_users = function(req, resp, db) {
  var User = db.model('User')
  var context = {
    'users':function(ready) { User.find({}, ready) }
  }
  template('user_list.html', function(err, tplInstance) {
    tplInstance.render(context, function(err, data) {
      resp.send(data)
    })
  })
}

And our template can look like this:

{% extends "base.html" %}

{% block content %}
{% for user in users %}
  <li>{{ user.name }}</li>
{% endfor %}
{% endblock %}

And you're pretty much done getting started!

Using Plate with Express

If you have express installed, plate will automatically have a compile method that can be registered with express. You will still need to configure your template loader.

// configure.js
var plate = require('plate')
  , Loader = require('plate/lib/plugins/loaders/filesystem').Loader
  , path = require('path')

module.exports = function configure(app, dir) {
  var plugin = new Loader([dir]).getPlugin()

  app.register('.html', plate)
  app.set('views', dir)
  app.set('view engine', 'html')
  plate.Template.Meta.registerPlugin('loader', plugin)
}
// app.js
var express = require('express')
  , plate = require('plate')
  , configure = require('./configure')
  , app

app = express.createServer()

configure(app, __dirname + '/views')

app.get('/', function(req, res){
  res.render('index', { layout: false, title: 'Plate Example' })
});

app.listen(3000);
console.log('Express app started on port 3000');
Something went wrong with that request. Please try again.