0
module.exports = (items) => {
return `
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sample</title>
</head>
<body>
  <table>
    <tr>
      // dynamic render
    </tr>
  </table>
</body>
</html>`
}

I want to render a dynamic array like items in the //dynamic render section how can I add it there ?

1 Answer 1

1

You can create the template and then replace whatever dynamic stuff you want to render in it like below

let template = `<!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Sample</title>
    </head>
    <body>
      <table>
        <tr>
         {{dynamicPart}}
        </tr>
      </table>
    </body>
    </html>`;
    
// assuming list is an array ['a', 'b', 'c']
module.exports = (items) => {
    let data = '';
    // creating dynamic data
    for (const item of items) {
        data += `<td>${item}</td>`;
    }
    // replacing placeholder with actual data and returning
    return template.replace('{{dynamicPart}}', data);
}
Sign up to request clarification or add additional context in comments.

2 Comments

can i replace more than one dynamic part in html ?
yes, just add more placeholders and more replace logic just like mentioned above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.