0

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.

Karan
  • 64
  • 5
  • 1
    Try using `<%= projectName %>` in your `base.ejs` file wherever you want to use it, I don't think you need to pass it as `{projectName: projectName}` as you have been trying – Richlewis Jan 15 '19 at 08:31
  • Yes, works perfectly. There was no need to pass it. Thanks! – Karan Jan 15 '19 at 08:39
  • No problem, I'll add it as an answer so that if others run into the same issue it is clear to see the solution – Richlewis Jan 15 '19 at 09:14

1 Answers1

1

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 %>
Richlewis
  • 15,070
  • 37
  • 122
  • 283