f1nn
f1nn

Reputation: 7047

NodeJS - inner dynamic templates in EJS Template engine

I'd like to create an inner dynamic template for menu header. I'd like to send parameter of current page for my inner menu template, located in main page template, and depending on url, it should show different menu items active. How can I do it? thanks

UPDATE I know about this:

<html>
  <head></head>
  <body>
    Blah blah blah
    <%- partial('menu') %>    
  </body>
</html>

But I have no ideas, how I can send parameter to inner template

Upvotes: 0

Views: 2013

Answers (1)

alessioalex
alessioalex

Reputation: 63663

ejs doesn't have partials, but Express does: http://expressjs.com/guide.html#view-partials

A simple example using Express and EJS:

app.js

var express = require('express'),
    app     = express.createServer();

app.configure(function() {
  app.set('view engine', 'ejs');
  app.use(express.methodOverride());
  app.use(express.bodyParser());
});

app.get('*', function(req, res, next) {
  res.render('list', { items: ['foo', 'bar', 'baz'], layout: false });
});

app.listen('9000');

views/list.ejs

<ul>
    <%- partial('item', items) %>
</ul>

views/item.ejs

<li><%= item %></li>

Warning: Express 3.x will not contain partials anymore, so it will be only up to the template rendering engine to do that.

Upvotes: 2

Related Questions