Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Express/EJS, how do I change the default layout?

I don't want it to be layout.ejs.

I want to set my layout to be another template for THIS specific controller.

like image 520
TIMEX Avatar asked Jul 22 '11 06:07

TIMEX


People also ask

What is EJS template?

EJS: EJS or Embedded Javascript Templating is a templating engine used by Node.js. Template engine helps to create an HTML template with minimal code. Also, it can inject data into HTML template at the client side and produce the final HTML.

What is locals in EJS?

Locals represent server-side data that is accessible to your view—locals are not actually included in the compiled HTML unless you explicitly reference them using special syntax provided by your view engine. <div>Logged in as <a><%= user.fullName %></a>.</

Is EJS part of Express?

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

Set custom default layout

Just set layout property in express app settings.

app.set('layout', 'layouts/layout');

Set custom layout for single render

Just pass layout as render locals object.

app.get('/', function(req, res) {
  res.render('the-view', { layout: 'specific-layout' });
);

More info here: https://www.npmjs.com/package/express-ejs-layouts

like image 125
Madhu Avatar answered Sep 24 '22 05:09

Madhu