1

There are a lot of similar questions, but I haven't understood how to apply them to my situation, so I'll try to be really specific. I have a very long tagged template literal, for example;

tag`This is a very long string that will be shown to ${userName}, that is annoying to have all on one line in the codebase.`

Of course, I don't want a line that long in my codebase, so I'd like to break it up. There are many options for breaking up a long (un-tagged) template literal, like using +. I tried to use this, but it messes with the operation precedence. If I use

tag`my` + `string`

Then the tag only applies to my. If I try (with or without a plus)

tag(`mystring`)

the functionality of the tag stopped working. As a sanity check, I opened my browser console and defined;

function myTag(strings) { return `${strings[0]}` }

If I used this as a tag with the normal syntax, it output the input string. If I used the parens, it output the first character of the input string. So I have no idea what's happening there.

I can use

tag`my\
string`

But that doesn't allow the second line to be indented, which is just as bad as a long line.

Given this, I don't know how to apply the methods for breaking up a long un-tagged template literal to a tagged template literal.

In case it ends up being relevant, the function I'm using is the t tag from the library ttag.

Guido Flohr
  • 1,871
  • 15
  • 28
Alex Altair
  • 3,246
  • 3
  • 21
  • 37
  • 1
    https://stackoverflow.com/questions/37321047/wrap-long-template-literal-line-to-multiline-without-creating-a-new-line-in-the – epascarello Apr 16 '20 at 22:41
  • @epascarello My question is about *tagged* template literals, and that question (and all the others I could find) are about template literals without tag functions. This is why it's not a duplicate of that question. – Alex Altair Apr 17 '20 at 02:43
  • @Barmar I would edit my question to improve it, but there are already many parts of my question that emphasize how it's different from the proposed duplicate, so I'm honestly not sure how I could do better. – Alex Altair Apr 17 '20 at 02:46
  • Do tagged template literals really work differently with respect to escaping the newline? Anyway, I've reopened. – Barmar Apr 17 '20 at 04:06
  • @Barmar I can still escape the newline, but that messes with indentation, which IMO is worse than a long line. The tag is what's preventing me from using other solutions. Thanks for reopening! – Alex Altair Apr 17 '20 at 04:43
  • 1
    I think all the requirements you're making may make it impossible. They didn't design template literals to satisfy this need. – Barmar Apr 17 '20 at 04:56
  • So your options are to break it up into multiple parts. `\`${foo}\` + \`${bar}\`` or live with one long line. – epascarello Apr 17 '20 at 13:13

0 Answers0