2

I'm a beginner in node.js.
My index.ejs file has an included header.ejs file. Everything works well except that I cant pass values to the variable status in header.ejs.

index.ejs

<html>
.
.
<title> <%= title %> </title>
.
.
<% include ../partial/header.ejs %>
.
.
</html>

header.ejs

<header>
.
.
<p>logged in status: <%= status %> </p>
.
.
</header>

app.js

.
.
.
app.get('/', function(req, res)
{
    // not working :(
    res.render('index', {
        "status":"loggedin",
        "title":"home"
    });
});
.
.
.
0

1 Answer 1

1

There's somewhat of a mess with your structure.

  1. <title> should be within <head>.
  2. <p> should be within <body>.
  3. Note that you may have confused <head> and <header> tags in your templates. You can learn about the difference here.

Here's an example I expect will work for you:

index.ejs:

<html>
<head>
    <title> <%= title %> </title>
</head>
<body>
    <% include ../partial/header %>
</body>
</html>

header.ejs:

    <p>logged in status: <%= status %> </p>
Sign up to request clarification or add additional context in comments.

3 Comments

thank you. I knew that and I'm not confused with header and head. I just shrieked it down to focus on my question. App.js renders index.ejs. But to render index.ejs it need header.ejs to render first. How can I do that? Any alternative methods?
@WWJ, try to remove the .ejs from your include syntax. In EJS you can include any template file as long as your node process has read access to it.
Yes, perfect. That is what exactly I want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.