Skip to content

Instantly share code, notes, and snippets.

@yamatt
Created December 23, 2013 18:40
Show Gist options
  • Select an option

  • Save yamatt/8102332 to your computer and use it in GitHub Desktop.

Select an option

Save yamatt/8102332 to your computer and use it in GitHub Desktop.
Simplification of an issue I had with Swig where you could not replace blocks conditionally when doing template inheritance.
var app = require("express")();
var swig = require('swig');
app.engine('swig.html', swig.renderFile);
app.set('view engine', 'swig.html');
app.set('views', __dirname);
app.get("/loggedin", function (req, res) {
res.render("default", {loggedin: true});
});
app.get("/loggedout", function (req, res) {
res.render("default", {loggedin: false});
});
app.listen(3000);
<!DOCTYPE html>
<html>
<head>
<title>Swig Conditional Template Inheritance</title>
</head>
<body>
{% block content %}
<p>Welcome loggedout user</p>
{% endblock %}
</body>
</html>
{% extends "base.swig.html" %}
{% if loggedin %}
{% block content %}
<p>You are logged in.</p>
{% endblock %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment