我正在尝试将一些代码从swig转换为ejs,但是面临着“for”循环的问题。
问题是swig文件中的循环正在通过config.json文件中的内容正确循环。然而,当我尝试在ejs中实现类似的代码时,我无法使其工作并需要一些指导。
我有一个config.json文件,我正在循环:
{
"server" : {
"port" : 3000,
"ssl" : false,
"ssl_cert" : "/home/pi/lirc_web/server.cert",
"ssl_key" : "/home/pi/lirc_web/server.key",
"ssl_port" : 3001
},
"repeaters": {
"lg": {
"KEY_VOLUMEUP": true,
"KEY_VOLUMEDOWN": true
}
},
"macros": {
"TESTTV": [
[ "lg", "KEY_VOLUMEUP" ],
[ "lg", "KEY_VOLUMEUP" ],
[ "lg", "KEY_VOLUMEDOWN" ]
]
},
"commandLabels": {
"lg": {
"KEY_VOLUMEUP": "Volume Up",
"KEY_VOLUMEDOWN": "Volume Down"
}
},
"remoteLabels": {
"lg": "LG TV"
},
"blacklists": {
}
}
route.js文件包含(代码段):
config = require('../config.json');
app.get('/profile', isLoggedIn, function(req, res)
{
app.get('/remotes.json', function (req, res) {
res.json(refineRemotes(lircNode.remotes));
});
var refinedRemotes = refineRemotes(lircNode.remotes);
res.render('remotes.ejs', {
macros: macrostwo,
remotes: refinedRemotes,
macros: config.macros,
repeaters: config.repeaters,
labelForRemote: labelFor.remote,
labelForCommand: labelFor.command,
});
console.log('DEBUG: ConfigFile Macros: \n', macrostwo);
});
正在运行的swig文件包含:
<ul class="macros-nav">
{% for macro in macros %}
{% set macroName = loop.key %}
<li><button class="btn btn-wide btn-large btn-warning macro-link" href="/macros/{{ macroName|url_encode }}">{{ macroName }}</button></li>
{% endfor %}
</ul>
正在处理的ejs文件包含以下代码:
<ul class="macros-nav">
<% macros.forEach(function(macro){ %>
<li><button class="btn btn-wide btn-large btn-warning macro-link" href="/macros/<%=macro.name%>"> <%=macro.name%> </button></li>
<% }) %>
</ul>
如果我更改我的routes.js文件以将下面的代码传递给ejs视图,则foreach循环正常工作
var macrostwo = [
{ name: 'Nathan Explosion', message: 'Dethklok rules' },
{ name: 'William Murderface', message: 'Bass is the heart of the band' },
{ name: 'Dr. Rockso', message: 'Where is my friend Toki?' }
];
app.get('/profile', isLoggedIn, function(req, res)
{
app.get('/remotes.json', function (req, res) {
res.json(refineRemotes(lircNode.remotes));
});
var refinedRemotes = refineRemotes(lircNode.remotes);
res.render('remotes.ejs', {
macros: macrostwo,
remotes: refinedRemotes,
macros: macrostwo, //config.macros,
repeaters: config.repeaters,
labelForRemote: labelFor.remote,
labelForCommand: labelFor.command,
});
console.log('DEBUG: ConfigFile Macros: \n', macrostwo);
});
EDIT1:
我一直试图让他成为对象。但是我在打印到控制台时只得到“1”。我知道我在ejs文件中使用了macros.name,它在config.json中是“非”的。问题是我不知道如何在循环低谷时获取密钥名称。
/托马斯
答案 0 :(得分:0)
找到解决方案:
route.js:
app.get('/profile', isLoggedIn, function(req, res)
{
app.get('/remotes.json', function (req, res) {
res.json(refineRemotes(lircNode.remotes));
});
console.log('=============\n');
var keys = Object.keys(config.macros);
console.log(keys);
console.log('\n=============\n');
var refinedRemotes = refineRemotes(lircNode.remotes);
res.render('remotes.ejs',{
remotes: refinedRemotes,
macros: keys, //config.macros,
repeaters: config.repeaters,
labelForRemote: labelFor.remote,
labelForCommand: labelFor.command,
});
});
ejs文件:
<ul class="macros-nav">
<% macros.forEach(function(macro){ %>
<li><button class="btn btn-wide btn-large btn-warning macro-link" href="/macros/<%=macro%>"> <%=macro%> </button></li>
<% }) %>
</ul>