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"
    });
});
.
.
.
WWJ
  • 23
  • 1
  • 6

1 Answers1

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>
Community
  • 1
  • 1
Selfish
  • 6,023
  • 4
  • 44
  • 63
  • 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 Sep 27 '15 at 17:16
  • @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. – Selfish Sep 27 '15 at 18:20
  • Yes, perfect. That is what exactly I want. – WWJ Sep 28 '15 at 01:01