Search code examples
node.jsrubyexpresshaml

How do I get HAML layouts working with haml-coffee and Express 4?


I followed "NodeJS Express app generation with CoffeeScript and HAML" and the express-usage docs, but my layout is not displaying.

I can see the rendered index.hamlc OK, but it doesn't contain the layout. How do I get layout to work with HAML and Express 4?

server.js:

const express = require('express'),
    partials = require("express-partials"),
...
app.set('views','app/views');
app.engine("hamlc", require("haml-coffee").__express)
app.use(partials())
app.set("view engine", "hamlc")

index.js:

router.get('/', (req, res) => {
    res.render('index', {name: "User"})

app/views/layout.hamlc:

!!!
%head
  %title Express App
%body
  xxx
  != @body

I viewed the source in Chrome using view-source:http://127.0.0.1/ and it only shows the template contents but not the layout.


Solution

  • I moved the line

    app.use('/', indexRouter)
    

    below

    app.engine("haml", require("haml-coffee").__express)
    app.use(partials())
    app.set("view engine", "haml")
    

    and it worked. I didn't think the order would matter. (I also renamed the files .haml to match conventions and enable syntax hilighting.)