1

So I have been fine adding a home button to the rest of my pages on my app. I want to add a title that goes with each page, but keep the home button code compartmentalized.

I thought it might be possible to send variables through using the include notation, but it is not working. I know my file system is correct, because when I take out the second argument to the include function, everything works fine.

This is the current code in where I want to have the home button, with a custom passable title.

<%- include("../partials/homeButton.ejs", { title: "page title" });%>

And the code I have in homeButton.ejs is simply

<% if(typeof(title) === 'undefined') title = "no title included" %>
<h1 id="homeButton" class="hover-underline montserrat-medium fs-2rem"><%= title %></h1>

It is simply not recognizing the title variable as a thing. It says title is undefined, and because of my if statement in homeButton.ejs, it always displays, "no title included".

Kaspar Poland
  • 105
  • 1
  • 9
  • 1
    Do you have a [mcve] to show off the problem? Based on the code you're showing, this will work fine, so without a demonstrator, there isn't really anything to comment on. You could try to just define `title`, because EJS doesn't isolate includes, they have access to the entire render context (e.g. [like this](https://stackoverflow.com/a/29233233/740553)), but other than that, this looks fine. – Mike 'Pomax' Kamermans Jul 31 '19 at 16:19
  • that is why I am so confused. This is all the code that is being worked on and is doing anything. I changed it from <% include ../partials/homeButton.ejs %> to what it is now, and it broke... – Kaspar Poland Jul 31 '19 at 16:24
  • 1
    Unlikely to be it, but: try single quotes for the partial's path. (simply because the ejs docs only ever show single quotes for ejs's own calls) – Mike 'Pomax' Kamermans Jul 31 '19 at 16:48
  • 1
    It works in my console, getting this `

    page title

    ` as my html. Can you paste how you are rendering the ejs?
    – Aritra Chakraborty Jul 31 '19 at 18:05
  • @AritraChakraborty For some reason, this fixed itself. I am not sure what the issue was. Thank you for trying to help! – Kaspar Poland Aug 02 '19 at 16:27

0 Answers0