Search code examples
node.jsexpresstemplate-engine

Template engine for express 4 supporting Layouts


I'm looking for alternatives to Jade templates in express 4.x because I really don't like Jade's syntax. I'm tending towards EJS, because it's basically just HTML on steroids.

However, one really nice feature of Jade templates is the ability to use layouts. I've found https://www.npmjs.org/package/express-ejs-layouts, but it seems to be made for express 3 and its build is failing :/.

I also found https://www.npmjs.org/package/ejs-mate which is made for express 4.x but it only seems to support a single content block (body).

I would like to have something like this:

layout.something:

<html>
    <head>
        <% block styles %>
        <% block scripts %>
    </head>
    <body>
        <% block body %>
    </body>
</html>

index.html:

uses layout "layout.somehing"
scripts:
    <script src="my_custom_script.js"></script>

styles:
    <link rel="stylesheet ...></link>

body:
    <h1>This is my body!</h1>

So that this yields:

<html>
    <head>
        <link rel="stylesheet ...></link>
        <script src="my_custom_script.js"></script>
    </head>
    <body>
        <h1>This is my body!</h1>
    </body>
</html>

Does anyone know an engine that is capable of that besides Jade?


Solution

  • You can try express-handlebars, it supports layout and partial views.