Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layouts in Express 3 and EJS

In version 3 of Express some features were removed:

the concept of a "layout" (template engine specific now)
partial() (template engine specific)

Changelog: https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x

The partial() can be changed for EJS own feature called include, but what is the alternative for layouts?

like image 832
artvolk Avatar asked Sep 27 '12 08:09

artvolk


People also ask

What does Express EJS layouts do?

Today we are going to look at how we can use Express EJS Layouts to help us with website templating which ultimately help us to avoid writing duplicated code as well as making our website / application easily maintainable.

Is EJS and Express JS same?

Template Engines and EJS: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.


1 Answers

I struggled with this as well. So I put up a github project with an example for ejs and dustjs.

https://github.com/chovy/express-template-demo

I'm not sure the difference between a partial and an include, you don't need to explicitly pass data to an include. Not sure why you would want a partial.

But for a layout, you just specify a block like this:

//layout.ejs
<html>
<%- body %>
</html>

//page1.ejs
<% layout('layout') -%>
This is loaded from page 1 and overrides <%- body %> in the layout.ejs.

If anyone wants to add more examples, just submit a pull request.

like image 114
chovy Avatar answered Sep 23 '22 14:09

chovy