简体   繁体   中英

Pass variables to res.render in node.js?

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"
    });
});
.
.
.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM