Permalink
Browse files

Fix some issues with Express example and docs

Fixes #85
  • Loading branch information...
1 parent c93c561 commit a32fc90d78a5964350433c5444995a0bbe194f93 @paularmstrong committed Oct 20, 2012
Showing with 23 additions and 26 deletions.
  1. +10 −5 docs/express.md
  2. +12 −20 examples/express/server.js
  3. +1 −1 examples/express/views/variable.html
View
15 docs/express.md
@@ -3,33 +3,38 @@ Using Swig in Express for View Rendering <a name="express" href="#express">#</a>
Swig is compatible with [Express][1]! It's very easy to set up your Express-based node project to use Swig as its template renderer. Here's how:
+First, get Express and Consolidate.js
+
+ npm install express
+ npm install consolidate
+
Require `express`, `Swig`, and create the express server.
var express = require('express'),
+ cons = require('consolidate'),
swig = require('swig'),
app = express.createServer();
Register Swig as the template renderer.
- app.register('.html', swig);
+ app.engine('.html', cons.swig);
app.set('view engine', 'html');
Set up your views directory.
+ // NOTE: Swig requires some extra setup so that it knows where to look for includes and parent templates
swig.init({
root: '/path/to/views/directory/',
- allowErrors: true // allows errors to be thrown and caught by express
+ allowErrors: true // allows errors to be thrown and caught by express instead of suppressed
});
app.set('views', '/path/to/views/directory/');
**Important!** Don't allow express to automatically pipe your template into a `layout.html` file. Setting this to false allows you to properly use `{% extends %}` and `{% block %}` tags!
- app.set('view options', { layout: false });
-
Render your `index.html` page!
app.get('/', function (req, res) {
res.render('index.html', { foo: 'bar' });
});
-[1]:http://expressjs.com/
+[1]:http://expressjs.com/
View
32 examples/express/server.js
@@ -4,6 +4,10 @@ var express = require('express'),
app = express(),
people;
+// NOTE: It is preferred to use consolidate.js
+// However, we can't do that in this example, because the example uses
+// The uninstalled version of swig for testing purposes
+// Please see the documentation for proper use with Express
swig._cache = {};
swig.express3 = function (path, options, fn) {
swig._read(path, options, function (err, str) {
@@ -55,39 +59,27 @@ app.get('/', function (req, res) {
res.render('index', {});
});
-app.get('/sub', function (req, res) {
- res.render('sub', {});
-});
-
-app.get('/outer', function (req, res) {
- res.render('outer', {});
-});
-
-app.get('/middle', function (req, res) {
- res.render('middle', {});
+app.get('/people', function (req, res) {
+ res.render('people', { people: people });
});
-app.get('/inner', function (req, res) {
- res.render('inner', {});
+app.get('/people/:id', function (req, res) {
+ res.render('person', { person: people[req.params.id] });
});
app.get('/variable', function (req, res) {
res.render('variable', { extendFile : 'outer.html' });
});
+app.get('/*', function (req, res) {
+ res.render(req.params[0], {});
+});
+
people = [
{ name: 'Paul', age: 28 },
{ name: 'Jane', age: 26 },
{ name: 'Jimmy', age: 45 }
];
-app.get('/people', function (req, res) {
- res.render('people', { people: people });
-});
-
-app.get('/people/:id', function (req, res) {
- res.render('person', { person: people[req.params.id] });
-});
-
app.listen(1337);
console.log('Application Started on http://localhost:1337/');
View
2 examples/express/views/variable.html
@@ -1,4 +1,4 @@
-blah!{% extends extendFile %}
+{% extends extendFile %}
{% block body %}Extended Variable Template{% endblock %}

0 comments on commit a32fc90

Please sign in to comment.