how to delay interpolation of template strings?

Caitlin Potter caitpotter88 at gmail.com
Tue Dec 16 09:43:56 PST 2014


The compile() tag wouldn’t work the way tagged templates are currently. The `args` array would be initialized at compile()-time, and those same arguments would always be used no matter what you’d passed in to the returned function.

It would be cool if there were some sugar for this, but I don’t think tagged templates will get you there. Worse than that, you’d ideally be able to run “compiled” templates through tag functions (e.g. for markup sanitization), and it’s not clear how you’d do that from this. Maybe a new builtin like global.Template, maybe Reflect.compileTemplate, which would accept a template string as input (this wouldn’t really make sense unless it were somehow a “magic” function which processed templates differently though :()

> On Dec 16, 2014, at 12:30 PM, Alex Kocharin <alex at kocharin.ru> wrote:
> 
>  
> I think ES6 is more awesome than people usually realize. Oh by the way, here is some sugar:
>  
>     var template = compile`Hello, ${"name"}!`
>     console.log(template({ name: "world" }))
>  
>  
>  
> Of course, ES6 does not have a compile function built-in, but I surely have one:
>  
>     function compile(strs, ...args) {
>       return function(object) {
>         var chunks = []
>         for (var i=0; i<strs.length-1; i++) {
>           chunks.push(strs[i])
>           chunks.push(object[args[i]])
>         }
>         chunks.push(strs[i])
>         return chunks.join('')
>       }
>     }
>  
>  
>  
> 16.12.2014, 20:03, "Claude Pache" <claude.pache at gmail.com>:
>> 
>>> Le 16 déc. 2014 à 16:42, Matthew Robb <matthewwrobb at gmail.com <mailto:matthewwrobb at gmail.com>> a écrit :
>>> 
>>> Actually, it would be cool if some sugar could be done for the example I just did:
>>>  
>>> var template = compile`
>>> Hello ${{first_name}}
>>> `;
>>>  
>>> template({ first_name: "John" });
>>>  
>>  
>> I guess you want sugar for the following valid ES6 code?
>>  
>>  
>> ```javascript
>>  
>> var template = _ => `
>> Hello ${_.first_name}
>> `;
>>  
>> template({ first_name: "John" });
>>  
>> ```
>>  
>> —Claude
>>  
>> ,
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
>> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>_______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141216/7a780ecd/attachment.html>


More information about the es-discuss mailing list