Node.js - EJS - including only a part of a partial

Viewed 30

Currently working on a dynamic database website. I'm trying to include html elements from one .ejs file depending on a variable without having an .ejs file for every of these variables while also saving as much repetitve code as possible.

Example (the problem im facing)

in app.js:

const killerData = require(__dirname + '/js/killerdata.js');

app.get('/killer/:killerid', function(req, res){

  let killerid = (req.params.killerid);
  let killerInfo = killerData[killerid];

  res.render('killer', {
    killerid: killerid,
    killerName: killerInfo.name,
    killerMoveSpeed: killerInfo.moveSpeed,
    killerTerrorRadius: killerInfo.terrorRadius,
    killerVoiceActor: killerInfo.voiceActor,
    killerPerk1Name: killerInfo.perk1,
    killerPerk2Name: killerInfo.perk2,
    killerPerk3Name: killerInfo.perk3
  });
});

It works perfectly fine since I am working with objects in the killerdata.js file. Here an example:

exports.trapper = trapper = {
  name:"Evan McMillan",
  moveSpeed:"115% | 4.6m/s",
  terrorRadius:"32 metres",
  voiceActor:"Filip Ivanovic (BHVR)",

  perk1:"Unnerving Presence",
  perk2:"Brutal Strength",
  perk3:"Agitation",

  perk1Info:"",
  perk2Info:"",
  perk3Info:""
};

Now here I face the problem. Since the last 3 attributes (and many more coming) have to be html code, putting it into the string in the .js file would be an absolute mess. An very dirty to change or add more to it.

I thought about using the <% Javascript %> ejs tag to declare objects in an .ejs file with the needed html code snippets in it, but couldnt make it work.

Also making an ejs file for every of these attributes isnt an option since i would have to create over 300 .ejs files. And declaring even more attributes would also make a mess.

My dream scenario would be to have 1 ejs file with all the html snippets in it and export the one i need atm.

How do i achieve such a thing?

0 Answers
Related