Brian
Brian

Reputation: 1743

Issue getting Node.js Express 3.x template inheritance working with Swig via consolidate.js

I can get 'standalone' templates to render just fine with this code, but I can't get template inheritance to work. Is there something I am overlooking or any other caveats anyone knows of?

Error: Circular extends found on line 3 of "... /views/index.html"!

app.js:

var express = require('express')
  , cons = require('consolidate')
  , http = require('http')

var app = express();

app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view options', { layout: false });

app.get('/', function(req, res){
  res.render('index.html', { header: 'Express' });
});

http.createServer(app).listen(3000, function(){
  console.log("Express server listening on port 3000");
});

index.html

{% extends 'base.html' %}

{% block content %}<h1>{{ header }}</h1>{% endblock %}

base.html

<!DOCTYPE html>

<html>
    <head>
        <title>{% block title %}Express{% endblock %}</title>
    </head>

    <body>
        {% block content %}{% endblock %}
    </body>
</html>

Upvotes: 1

Views: 2071

Answers (2)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

You can resolve this by setting root and allowErrors for swig itself:

var express = require('express')
  , cons = require('consolidate')
  , http = require('http')
  , swig = require('swig')

swig.init({ root: __dirname + '/views', allowErrors: true });

// ...

For more info, see Using Swig with express.js and Swig API.

Upvotes: 3

chovy
chovy

Reputation: 75686

I'm not sure about swig, but in express3 they removed template inheritance, partials and layouts and left it up to the template engine to implement. There are plugins that might get it back for you.

ejs: https://github.com/RandomEtc/ejs-locals

https://github.com/publicclass/express-partials

Upvotes: 0

Related Questions