[英]Node.js ejs - How to use a variable in a ejs expression?
I'm using express and ejs to render my files.我正在使用 express 和 ejs 来呈现我的文件。 In a addition I render a <%= data.groupID %> which is 60113 like this:
此外,我渲染了一个 <%= data.groupID %> ,它是 60113 ,如下所示:
<div>
<%= data.groupID %>
</div>
<div>
<% include docs/profile_60113 %>
</div>
How can I use the variable in the <% include docs/profile_GROUPID %> expression?!如何在 <% include docs/profile_GROUPID %> 表达式中使用变量?!
<div>
<script>
var getGroupID;
function functiongroupID() {
getGroupID = '<%= data.groupID %>';
}
functiongroupID();
console.log(getGroupID);
</script>
</div>
<div>
<%= data.groupID %>
</div>
<div>
<% include docs/profile_GROUPID %>
</div>
Is that even possible or do I need to do an if?!这是可能的还是我需要做一个如果?!
The syntax for an include is:包含的语法是:
<%- include('user/show', {user: user}); %>
You can just put the variable where you would normally put the string literal.您可以将变量放在通常放置字符串文字的位置。
<%- include(variable_here, {user: user}); %>
And if you want to combine it with a string, you do so in any of the normal ways:如果你想把它和一个字符串结合起来,你可以用任何一种正常的方式来做:
<%- include("docs/profile_" + GROUPID); %>
<%- include(`docs/profile_${GROUPID}`); %>
Obviously, GROUPID
needs to be a variable generated in your server-side code and not in your client-side code since your client side <script>
element won't run until the EJS has finished executing and the output of it sent to the browser.显然,
GROUPID
需要是在您的服务器端代码中生成的变量,而不是在您的客户端代码中,因为您的客户端<script>
元素在 EJS 完成执行并将其输出发送到浏览器之前不会运行.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.