Layout.ejs not working in Nooks app in Heroku

I have a regular expressjs express application ...

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 index.ejs and layout.ejs in the / 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 displays the text "Hello world" without the surrounding layout.ejs shell. Works ejs. It may find the correct .ejs template, but it just ignores the layout. I also tried to explicitly add the layout file to the application.

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

All of this works great locally, but not 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 is there no joy on the layout ??? Thanks

+4
source share
3 answers

When deployed to Heroku, it installs npm for all of your dependencies; because you specified express> = 2.5.x, it will install the latest version 3.0.0_betax. Express 3 does not support layouts in ejs (yet).

To fix the removal of "> =" and specify the version of the express, which is located in your local version.

+3
source

I use express 3.x with ejs-locals and it works well. You just need 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

+4
source

As chovy said, ejs-locals can help you deal with this if you want to upgrade to Express 3.x. There is a github repository here that provides a boot project for Express 3.x, ejs and twitter bootstrap:

https://github.com/cacois/node-express-twitter-bootstrap

A good starting point for a new application or an example of using ejs layouts with Express 3.x.

-1
source

All Articles