简体   繁体   中英

Using Template Literals with Lodash _.template()

I am trying to use template literals with Lodash' _.template() to select a message from a JSON file based on a response from another function that determines open/close and it is not working for me.

Flow:

  • Function runs logic and returns open or close via opencloseResponse.openclose object (Assumed working and not displayed in my code snippet below)
  • Using Lodash _.template() I put messages.open or messages.close with the user's First Name as first_name
  • Message text is displayed

Working Example without Template Literal

messages.json

{
  "messages": {
    "open": "Hello ${ first_name }! We are open!",
    "closed": "Hello ${ first_name }! We are closed!"
  }
}

app.js

const _ = require('lodash');
const { messages } = require('messages.json');

const sendMessage = _.template(messages.open);
console.log(sendMessage({ first_name: 'Jeremy' }));

output

Hello Jeremy! We are open!

Failed Example with Template Literal

messages.json

{
  "messages": {
    "open": "Hello ${ first_name }! We are open!",
    "closed": "Hello ${ first_name }! We are closed!"
  }
}

app.js

const _ = require('lodash');
const { messages } = require('messages.json');

const sendMessage = _.template(`messages.${opencloseResponse.openclose}`);
console.log(sendMessage({ first_name: 'Jeremy' }));

output

messages.open

I'm hoping its something simple that I am missing. It's been driving me nuts all day. I appreciate any help!

You do not need template literals.

Just use bracket [] notation to access the message properties

const sendMessage = await _.template(messages[opencloseResponse.openclose]);

Lodash's templates and JavaScript's template literals are completely different animals. If you want to continue to use those JSON files, you wouldn't use JavaScript's template literals, you'd just use dynamic property access:

const sendMessage = _.template(messages[opencloseResponse.openclose]);
console.log(sendMessage({ first_name: 'Jeremy' }));

If you want to use template literals, the way you make them reusable is to wrap them in functions . For instance, instead of your messages.json , you might have a messages.js :

exports.messages = {
  "open": (first_name) => `Hello ${ first_name }! We are open!`,
  "closed": (first_name) => `Hello ${ first_name }! We are closed!`
};

then

const messages = require('./messages');

console.log(sendMessage(messages[opencloseResponse.openclose]('Jeremy')));

Or you could have the function accept an object as with your Lodash code if you preferred:

exports.messages = {
  "open": ({first_name}) => `Hello ${ first_name }! We are open!`,
//         ^----------^-------------------------------------------------- destructuring
  "closed": ({first_name}) => `Hello ${ first_name }! We are closed!`
//           ^----------^------------------------------------------------ destructuring
};

then

const messages = require('./messages');

console.log(sendMessage(messages[opencloseResponse.openclose]({first_name: 'Jeremy'})));

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