简体   繁体   中英

Fill variable in exported file

I am trying to export a file with email information and a variable of 'name' that will be replaced when importing it to another javascript file.

const emailTemplate = {
    subject: 'test',
    body_html: `
    <html>
    <title>
    hey guys
    </title>
    <body>
    Hey ${name}
    </html>
    `
}

module.exports = {
    emailTemplate 
}

However, I am not sure how I can fill the name variable when importing it somewhere else, and haven't been really able to look it up. Any ideas on what I could do in this case? Thanks!

This is how I import it in the other file.

const emailTemplate = require('./email/template')

You can export a function to output the html instead.

function getBodyHtml(name) {
    // your desired content
    return `
<html>
<title>
hey guys
</title>
<body>
Hey ${name}
</html>`
}

const emailTemplate = {
    subject: 'test',
    getBodyHtml,
}

module.exports = {
    emailTemplate,
};

Then, call the function in another file.

// anotherFile.js
const emailTemplate = require('./email/template');

// call the function with the `name` variable in order to get the template
const myTemplate = emailTemplate.getBodyHtml('insert name here');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM