-1

Suppose a function greet(template) that greets a user given a greeting template.

So, template would be a string like: "Hello, {{name}}", and the function greet would have something like:

var message = template.replace("{{name}}", user.name)

In this example, I am using {{name}} and .replace as key concepts of the method. But is this the best way to do it?

What is the best syntax for the "string template" and the best way to process it? Keep in mind the only place I will use this template is in this function, so, I don't need a generic way to do it (In other words, things could be hardcoded).

Edit: I am looking for a genric way. I am trying to avoid template literals

  • If you don't want it to be more generic/powerful, the answer is yes, `replace` is the best way. Otherwise, consider a "real" templating engine like mustache. – georg Sep 19 '19 at 22:21
  • Is the template passed to your `greet` function (and could have any value), or is it really part of the function body (and not passed as an argument like your first sentence suggests)? – Bergi Sep 19 '19 at 22:23
  • @Bergi. The template will be passed as an argument and can be any string – Hiago Lucas Sep 19 '19 at 22:31
  • 1
    Asking for "the best way" is asking for an opinion. State your problem, show what you've done to try to solve it, and ask about any issues you have. Let people answer. They will answer with "the best" answer they know. Then you get to choose which one is "the best" for you. – Taplar Sep 19 '19 at 22:39
  • @Taplar Asking for the best way is asking for a solution that is simple, efficient and cover all cases. It is not an opinion. – Hiago Lucas Sep 19 '19 at 22:48
  • @georg. You are probally right. I dont want to be powerful as a template engine, so, `replace` looks good. – Hiago Lucas Sep 19 '19 at 22:50
  • What do you consider simple? Efficient? What are your cases? Without the details, yes, it is asking for an opinion. Without the criteria by which we know what an answer should consist of, that **you** are looking for, there is not a positive way for us to answer you with the idea being we will satisfy what you are looking for. – Taplar Sep 19 '19 at 22:51
  • 1
    @HiagoLucas: for a slightly more flexible solution see https://stackoverflow.com/questions/41117799/string-interpolation-on-variable/41118285#41118285 – georg Sep 19 '19 at 22:52
  • @georg Your last comment answers my question. Do you want to append a new answer to this topic? This way I will be able to check it as the best answer? – Hiago Lucas Sep 20 '19 at 12:18
  • @HiagoLucas: no need for that, let's just mark your topic as a duplicate. – georg Sep 20 '19 at 12:45

1 Answers1

1

using template literals

console.log(`Hello, ${name}`)
var message = template.replace(`${name}`, user.name)
Heera
  • 180
  • 12
  • No need for `template.replace` any more. I think you meant to write ``var message = `${user.name}`;`` – Bergi Sep 19 '19 at 22:21
  • In my context `template` will be passed by a json, so, template literals would not be possible. I am looking for a more genric way using strings. I will update the question – Hiago Lucas Sep 19 '19 at 22:25
  • 1
    @HiagoLucas cool cool, what's the output you are expecting...? – Heera Sep 19 '19 at 22:36