简体   繁体   中英

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>

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM