0

I have a function for template literals and when using it like this I want to get the variable name which is here 'count' as string inside my html template string function, is there any way?

const count = 0;

html`<h3>Count: ${count}</h3>`;

Get 'count' as string here, or even the whole unprocessed string like "<h3>Count: ${count}</h3>"

export function html(literals, ...vars) {
  // get function and variable names like 'count' here
  console.log(literals.raw);
  console.log(...vars);
}
inux
  • 99
  • 7
  • This question seems to have been asked before. See: https://stackoverflow.com/questions/31293928/how-to-get-the-raw-version-of-a-template-string-in-iojs – Deor Jun 14 '22 at 07:04
  • As I show [here](https://stackoverflow.com/a/70805136/3001761) it's pretty easy to write a "tag" that shows exactly what's received, and prove to yourself it's the values not the expressions that evaluated to them. – jonrsharpe Jun 14 '22 at 07:12
  • No, this is not possible. The tag function is passed the value `0`, not the name of the variable or expression that was used to compute it. – Bergi Jun 14 '22 at 08:09
  • @jonrsharpe your example does not show the variable names used in the template sorry. – inux Jun 14 '22 at 08:35
  • Yes, that's my point - the function receives the _values_, not the names. – jonrsharpe Jun 14 '22 at 08:35
  • @jonrsharpe oh I thought you mean your example works, well I guess not possible I find this kind of a missing feature, you should be able to get also a version of the whole unprocessed version inside the function. – inux Jun 14 '22 at 08:37

0 Answers0