Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 272
DeX3 Avatar asked Oct 09 '14 19:10

DeX3


People also ask

What template engine does Express use?

express-tl: A template-literal engine implementation for Express. Twing: First-class Twig engine for Node. js. Sprightly: A very light-weight JS template engine (45 lines of code), that consists of all the bare-bones features that you want to see in a template engine.

What is the best templating engine?

Mustache. Mustache is one of the most widely known templating systems that works for a number of programming languages, including JavaScript, Node. js, PHP, and many others. Because Mustache is a logic-less templating engine, it can be literally used for any kind of development work.

Is Express a template engine?

Template engine is a part of Express that enables us to use static files in our applications. Template engine converts variables to values and changes the template to HTML files to send to the client. The default template engine of Express is Jade, but EJS is the most commonly used engine.

Is EJS a template engine?

EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.


1 Answers

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

like image 161
Tien Do Avatar answered Sep 22 '22 06:09

Tien Do