how to delay interpolation of template strings?

Caitlin Potter caitpotter88 at gmail.com
Tue Dec 16 10:01:14 PST 2014


The main issue with it is, because of how you write it, you can’t really do anything useful with it. For instance, you can’t run a “compile()”’d template through a tag function — you can’t use useful expressions in template expressions, and in the current design there’s no real way around that, unless you’re willing to use `eval()` or `new Function()` to accomplish it.

> On Dec 16, 2014, at 12:55 PM, Alex Kocharin <alex at kocharin.ru> wrote:
> 
>  
>  
> 16.12.2014, 20:44, "Caitlin Potter" <caitpotter88 at gmail.com>:
>> 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.
>  
> That's funny, because my code works already in the FireFox 34. Pay attention to WHAT exactly is passed to the compile 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 <mailto: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 <mailto: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 <mailto:es-discuss at mozilla.org>
>>> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141216/0d9ca395/attachment.html>


More information about the es-discuss mailing list