Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout.ejs doesn't work on NodeJS app in Heroku

I have the usual nodejs express app...

var express = require('express');

var app = express.createServer(
    express.bodyParser()
);

app.configure( function () {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use("/public", express.static(__dirname + '/public'));
});

app.get('/', function (req, res) {
    res.render('index');
});

I have a index.ejs and layout.ejs in /views folder:

layout.ejs:

<!doctype html>
<html lang="en" manifest=""><head>
    <title>jQuery Plugin Demo</title>
</head>
<body>
    <div class="container container-fluid">
        <%- body %>
    </div>
</body>
</html>

index.ejs:

Hello world

index.ejs only renders the "Hello world" text without the surrounding layout.ejs wrapper. The ejs is working. It's able to find the correct .ejs template, but it's just ignoring the layout. I've also tried explictly adding layout file to app..

app.set('view options', { layout:'layout.ejs' }); 

All of this works fine locally, but not on Heroku. Here is my package.json:

{
  "name": "in1-test",
  "version": "0.0.1",
  "author": "Iatek",
  "dependencies": {
    "express": ">=2.5.x",
    "ejs": ">=0.7.x"
  },
  "engines": {
    "node": "0.6.x"
  }
}

Why no joy on the layout??? Thanks

like image 680
Zim Avatar asked Mar 26 '26 23:03

Zim


2 Answers

I'm using express 3.x with ejs-locals and it works well. You just have to specify which layout to use:

login.ejs

<% layout('layout') -%>
<form>...</form>

layout.ejs

<body>
<h1>Hello</h1>
<%- body %>
</body>

https://npmjs.org/package/ejs-locals

like image 63
chovy Avatar answered Mar 29 '26 19:03

chovy


When you deploy to Heroku it does an npm install for all your dependencies; because you have stated express >=2.5.x it will install the latest which is 3.0.0_betax. Express 3 does not have support for layouts in ejs (yet).

To fix remove the ">=" and specify the version of express that is in your local version.

like image 25
rogchap Avatar answered Mar 29 '26 20:03

rogchap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!