Question: nodejs foreach swig and ejs

Question

nodejs foreach swig and ejs

Answers 1
Added at 2016-12-22 10:12
Tags
Question

I'm trying to convert some code from swig to ejs, but are facing an issue with a "for" loop.

The problem is that the loop inside the swig file is looping trough the content from the config.json file correctly. However when i try to implement similar code in ejs i cant get it to work and need some guidance.

I have a config.json file i'm looping trough:

{
  "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": {     
  }
}

The route.js file contains (code snippet):

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);
});

The swig file, which is working contains:

  <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>

The ejs file im working on have below code:

<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>

If i change my routes.js file to pass in below code to ejs view the foreach loop is working correctly

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:

I have been trying to get he object.keys but i just get "1" when printing to the console. I know i use macros.name in the ejs file, which is "non" existing in the config.json. The issue is that i dont know how to get the key names when looping trough.

/Thomas

Answers
nr: #1 dodano: 2016-12-22 14:12

Found a solution:

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 file:

<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>
Source Show
◀ Wstecz