我是node.js的初学者。我的index.ejs文件包含一个包含的header.ejs文件。除了无法将值传递给status中的变量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"
    });
});
.
.
.

最佳答案

您的结构有些混乱。


<title>应该在<head>之内。
<p>应该在<body>之内。
请注意,您的模板中的<head><header>标记可能会混淆。您可以了解差异here


我希望这是一个适合您的示例:

index.ejs:

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


header.ejs:

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

关于javascript - 将变量传递到node.js中的res.render吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32808083/

10-16 06:09