0

With ES6 we can create "tag functions" which operate on a template string. Is it possible to call the "default" tag function from within another one (that is, the one that is used when there is no prefix before the backtick e.g.

function html(template, ...substitutions)
{
  substitutions= substitutions.map(s=>SafelyEscapeStringForHtml(s));
  return DefaultTagFunction(template, ...substitutions);
}

the upshot of which is that my tag function doesn't need to deal with the concatenation and ensuring the parameters are inserted into the correct locations in the template. (Sure this isn't hard, but it's ugly and I don't want to see it in every tag function!)

DJL
  • 2,060
  • 3
  • 20
  • 39
  • Agree that it's a duplicate. Annoying that the linked question did not appear in my searches! – DJL Feb 17 '17 at 18:50

2 Answers2

1

Your own answer came close - you can abuse String.raw to give you what you want (with interpreted escape sequences):

function html(template, ...substitutions)
{
  substitutions= substitutions.map(s=>SafelyEscapeStringForHtml(s));
  return String.raw({ raw: template }, ...substitutions);
}

This is essentially just tricking it into thinking that the escape-interpreted string is the raw version.

Inkling
  • 3,544
  • 4
  • 30
  • 44
0

String.raw() is the default tag function

So the above code can be written as

function html(template, ...substitutions)
{
  substitutions= substitutions.map(s=>SafelyEscapeStringForHtml(s));
  return String.raw(template, ...substitutions);
}
DJL
  • 2,060
  • 3
  • 20
  • 39