1

I'm learning nodejs at the moment and my .ejs template files are pretty much standard (header, footer, js-defaults etc) - and the only thing that changes it the content in a HTML div.

I thought I could set a variable in my route and pass it along to the view (like you would a title or another variable), then include it - but it's not working (example below).

In Ruby you can do this with "yield" and I am trying to do the same with EJS.

Thanks for taking the time to read (and please forgive my ignorance in this matter).

Example Route

app.get('/fish', function(req, res) {

res.render('fish' {
    title:"Fish",
    template:"view/partials/content/fish.ejs"
});

});

Example EJS

<!DOCTYPE html>
<html>
<head>
  <title><%= title %></title>
</head>
<% include views/partials/template/header.ejs %>
<body>
    <div class="container">
      <!-- Dynamic content here -->
      <% include template %> <!-- tries to load template.ejs %>
    </div>
</body>
<% include views/partials/template/footer.ejs %>
<% include views/partials/template/js-defaults.ejs %>
</html>
ash
  • 1,224
  • 3
  • 26
  • 46
  • use `<%= title %>` instead `<% title %>` to output it to html, as in rails (.erb). – Ravi Dec 29 '14 at 12:48
  • Thanks @Ravi - meant to put that in my example. It's the dynamic content I am stuck with though - not sure how to get it to work. – ash Dec 29 '14 at 13:24
  • 1
    Well, i don't know is this supported yet or not, but check this out, https://github.com/tj/ejs/issues/93. – Ravi Dec 29 '14 at 13:48
  • Thank you again @Ravi - maybe I should use Jade instead then (not a fan personally) - +1 for your help though :) – ash Dec 29 '14 at 14:53

1 Answers1

2

Looks like this is now supported in EJS. It was introduced in v2.0.1: 2015-01-02. The new syntax looks like this

 <!-- Dynamic content here -->
      <%- include(template) %>

To get this to work I had to disable caching in Express by setting:

app.set('view cache', false);
ahoffer
  • 6,347
  • 4
  • 39
  • 68