Centell
Centell

Reputation: 409

ejs template expression in ejs template expression (not include)

My node version is v10.2.1, express version is 4.16.0, ejs version is 2.5.7.

I want to be able to change the name of the layout at a later time.

So I routing like this:

router.get('/', function(req, res, next) {
  res.render('index', { title: 'my homepage' , layout: 'basic'});
});

and index.ejs

And I wrote an include in the index like this:

<% include ./layouts/basic/header.ejs %>

Now, I want to change basic to <%= layout %> but,

<% include ./layouts/<%= layout>/header.ejs %>

causes:

Could not find matching close tag for "<%".

And,

<% include ./layouts/%><%= layout %><%/header.ejs %>

causes:

 Could not find the include file "./layouts/"

How can I solve it?

Upvotes: 0

Views: 582

Answers (1)

Mr. Ratnadeep
Mr. Ratnadeep

Reputation: 621

I am considering that you required two header views

  • horizontal header
  • vertical header

So inside views directory I have created the respective directories.
index.ejs

  <!DOCTYPE html>
    <html>
      <head>
        <title><%= title %></title>
        <link rel='stylesheet' href='/stylesheets/style.css' />
      </head>
      <body>
        <%if(layouts=='horizontal'){%>
            <%include ./horizontal/header%>
        <%}%>
        <%if(layouts=='vertical'){%>
            <%include ./vertical/header%>
        <%}%>
      </body>
    </html>

Here I have passed layouts variable from the routes. Hope it helps you to resolve the issue.

Upvotes: 2

Related Questions