Elegant way to generate string from tagged template?

Glen Huang curvedmark at gmail.com
Wed Dec 24 19:49:51 PST 2014


> That is unclear

I just want to minify the passed html string, but I got this incorrect mental model that tagged templates are just passing a string to a function. (And my incorrect mental model somehow gets corrected when considering params of that function. Yep, i’m inconsistent by nature :). I guess this comes from my write-as-little-as-possible OCD. :)

> but it's clear to me that template strings in ES6 (including String.raw) are expressive enough to do what you need. Confirm?

You question got me thinking: String.raw({raw: templateObj}, …args) looks like a hack. if this is the solution es6 is offering. I’m a bit worried: the expression itself doesn’t convey developer’s intent very well.

I also start to think about use cases for tagged templates and whether developers are really likely to generate final strings directly from template objects (and use this hack a lot).

From what I read:

1. Get unescaped strings

This what String.raw is for. "new RegExp(String.raw`\s+${dynamic}`)” looks very appealing. So manual generation not needed here.

2. I18n

People probably need to generate a sprintf-like format string first: “Hello, %s” from the template object, and then get “Bonjour, %s” from a translation table, then replace the placeholders with args.

In this case, the final string is not generated directly from the template object. So hacking String.raw might not be needed very often, probably not a terrible idea.

However, this is does raise the question that is it a good idea to add a sprintf-like function to es6?


> On Dec 25, 2014, at 2:39 AM, Brendan Eich <brendan at mozilla.org> wrote:
> 
> Glen Huang wrote:
>> I think I had been doing the whole thing wrong.
> 
> That is unclear, but it's clear to me that template strings in ES6 (including String.raw) are expressive enough to do what you need. Confirm?
> 
> /be



More information about the es-discuss mailing list