我在后端使用带有nodejs的ejs。 我想在包含时传递变量。包含标题时,传递页面标题。
index.ejs:
<% include header %>
<body> . . . </body>
<% include footer%>
header.ejs:
<html lang="en">
<head>
<title><%- my_tytle %></title>
</head>
footer.ejs:
</html>
如何在include命令中传递my_title
?
答案 0 :(得分:11)
您可以在include语句
中传递对象 <%- include("header",{title:"your_title"}) %>
答案 1 :(得分:4)
您可以将my_tytle
直接传递给index.ejs,如果有标题的部分视图,则标题可以访问my_tytle
。
例如: index.ejs:
<% include header %>
<body> . . . </body>
<% include footer%>
header.ejs:
<html lang="en">
<head>
<title><%- my_tytle %></title>
</head>
现在从节点服务器,如果您将my_tytle
的值传递给index.ejs,如下所示:
res.render('template_file.js', {
my_tytle : "Value for your title"
});
然后您的部分视图(即您的情况下的标题)也可以访问该变量。
答案 2 :(得分:0)
正如评论中的某处所问,我们还可以使用一个变量来由服务器控制,这是如何做的:
<%- include("header",{my_tytle :`${myVar}`}) %>