Karan
Karan

Reputation: 74

How to include an ejs with some parameters?

I have a file, let's say index.ejs which I render using express this way:

res.render('index.ejs', {
     projectName: req.params.name
}

Inside this ejs file, I include another file, let's say base.ejs. I'm trying to pass the variable projectName to base.ejs. I have tried the following approaches:

<%- include("path/to/base.ejs", {projectName: projectName})" %>
<%- include("path/to/base.ejs", {projectName: <%=projectName%>})" %>
<%- include("path/to/base.ejs", {projectName: '<%=projectName%>'})" %>

None of them seem to work. This is a similar answer I found how to include a template with parameters in EJS? but it doesn't seem to solve my problem.

Upvotes: 1

Views: 547

Answers (1)

Richlewis
Richlewis

Reputation: 15384

You don't actually need to pass the variable to the include statement as you have been doing, just use the variable in your base.ejs file as follows

<%= projectName %>

When using the include statement you can simply declare

<% include path/to/base %>

Upvotes: 1

Related Questions