node.js ejs 中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16005021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
functions in ejs
提问by Joshua
What I'd like to have is something like this:
我想要的是这样的:
app.js(node process, includes etc excluded for brevity but using ejs as rendering engine):
app.js(节点进程,包括为简洁起见排除的等,但使用 ejs 作为渲染引擎):
app.get('/', function(req, res){
var ejsVariables = {
title : 'ideal ejs function example',
listData1 : {
listTitle : 'my list',
listItems : [
{ name : 'first item', class : 'foo' },
{ name : 'second item', class : 'bar' },
{ name : 'last item', class : 'foo' } ]
},
listData2 : {
listTitle : 'your list',
listItems : [
{ name : 'a widget', class : 'foo' },
{ name : 'another widget', class : 'baz' }
]
}
};
res.render('index', ejsVariables);
});
index.ejs:
索引.ejs:
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1><%= title %></h1>
<% makeList(listData1) %>
<p>lorem ipsum</p>
<% makeList(listData1) %>
</body>
</html>
???
???
result:
结果:
<html>
<head>
<title>ideal ejs function example</title>
</head>
<body>
<h1>ideal ejs function example</h1>
<ul>
<li class="foo">first item</li>
<li class="bar">second item</li>
<li class="foo">another item</li>
</ul>
<p>lorem ipsum</p>
<ul>
<li class="foo">a widget</li>
<li class="baz">another widget</li>
</ul>
</body>
</html>
Question : what goes in the ??? section and/or should be changed above?
问题:里面有什么???部分和/或应该在上面更改?
What I've tried so far
到目前为止我尝试过的
Attempt 1
尝试 1
- 我真的很想让它起作用,但它不起作用index.ejs
索引.ejs
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1><%= title %></h1>
<% var makeList; %>
<!-- this variable is declared here as those declared within the include are not
accessible from the file from which it is called, I'm guessing some sort of
function scope is coming into play here -->
<% include makeList %>
<% makeList(listData1) %>
<p>lorem ipsum</p>
<% makeList(ListData2) %>
</body>
</html>
makeList.ejs
makeList.ejs
<% function makeListItem(itemData){ %>
<li class="<%= itemData.class %>" ><%= itemData.name %></li>
<% } %>
<% makeList = function(data){ %>
<% if(data){ %>
<ul>
<% data.map(makeListItem) %>
</ul>
<% } %>
<% } %>
In this situation both makeListItemand makeListare being called, it just appears that due to scoping or something else, when they come to be called they are unable to actually output to the template.
在这种情况下,makeListItem和makeList都被调用,只是由于作用域或其他原因,当它们被调用时,它们无法实际输出到模板。
Attempt 2
尝试 2
- 这确实有效,我只是不喜欢我最终使用包含而不是某种函数调用的方式。index.ejs
索引.ejs
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1><%= title %></h1>
<% var data = listData1 %>
<% include makeList %>
<p>lorem ipsum</p>
<% var data = listData2 %>
<% include makeList %>
</body>
</html>
makeList.ejs
makeList.ejs
<% function makeListItem(itemData){ %>
<li class="<%= itemData.class %>" ><%= itemData.name %></li>
<% } %>
<% if(data){ %>
<ul>
<% data.map(makeListItem) %>
</ul>
<% } %>
Attempt 3
尝试 3
- 这基本上涉及乱搞 ejs-localsejs-locals,但是我发现该功能有些缺乏。If I'm going to include another npm module in my application I'd really want it to be a more complete solution, but even then in an ideal world I'd rather do without and write it myself.
如果我打算在我的应用程序中包含另一个 npm 模块,我真的希望它是一个更完整的解决方案,但即使在理想的世界中,我也宁愿不这样做并自己编写它。
Apologies for the length of this post, it got out of hand quite quickly. I commend you if you've got this far.
为这篇文章的长度道歉,它很快就失控了。如果你做到了这一点,我赞扬你。
Any input would be greatly appreciated.
任何投入将不胜感激。
回答by robertklep
How about this:
这个怎么样:
// app.js
var ejs = require('ejs');
var fs = require('fs');
app.locals({
makeList : function(list) {
var template = fs.readFileSync('makeList.ejs', 'utf-8');
return ejs.render(template, list);
}
});
// index.ejs
<%- makeList(listData1) %>
^ important!
// makeList.ejs
<ul>
<% listItems.forEach(function(item) { %>
<li class="<%= item.class %>"><%= item.name %></li>
<% }); %>
</ul>
回答by andresgottlieb
The only thing I can think of (at least for the first example) is the makeList() function removing listData1 from ejsVariables, can you show me makeList() for that example?
我唯一能想到的(至少对于第一个例子)是 makeList() 函数从 ejsVariables 中删除 listData1,你能告诉我这个例子的 makeList() 吗?
In attempt 1, you're calling makeList() without a parameter!
在尝试 1 中,您调用 makeList() 没有参数!

