Template strings as a template language.

Thomas thomasjamesfoster at bigpond.com
Sun Sep 13 15:33:08 UTC 2015


Mark,

I'll put together a GitHub gist or repo and send it to you in the morning (I should be asleep) to make things clearer.

I'll try explaining it differently in the meantime:
What I'd like to do is use ES6 Template string as a templating language. For the sake of readability and maintainability, it would be nice to keep the templates separate from the code that uses them, and not have them inlined. 

The best solution for my use case would be to have a special eval function which would evaluate a normal string variable as if it was a template string, in the current scope. 

Thomas

> On 14 Sep 2015, at 1:10 AM, Mark S. Miller <erights at google.com> wrote:
> 
> The great achievement of modern js module systems including the es6 std module system is to end the greatest pain of prior js: linkage through side effects to the shared global scope. Good riddance.
> 
> If you're talking about a scope other than the global one, how would a template come to be evaluated in that scope? If the answer is that it is textually nested in that scope, then isn't that what template strings already do?
> 
> 
> 
> 
> 
>> On Sun, Sep 13, 2015 at 7:24 AM, Thomas <thomasjamesfoster at bigpond.com> wrote:
>> They would be whatever they are in the scope in which the template string is evaluated. 
>> 
>> Thomas
>> 
>>> On 14 Sep 2015, at 12:15 AM, Mark S. Miller <erights at google.com> wrote:
>>> 
>>> 
>>> 
>>>> On Sun, Sep 13, 2015 at 7:08 AM, Thomas <thomasjamesfoster at bigpond.com> wrote:
>>>> What I've been doing:
>>>> 
>>>> export const template = ({title, content}) => `template string for ${title}`;
>>>> 
>>>> Or variations thereof. I then import that module wherever I need to use the template and call it as a function.
>>> 
>>> If you were not to call it as a function, where would it get its bindings for title and content?
>>> 
>>>  
>>>> 
>>>> Using eval and having the template string as a normal string (so, read the template from file as a string, wrap it with back ticks, and then pass it to eval) at the moment is risky since it's possible for input to prematurely end the template string and do nasty stuff*. Ideally there would be a variant of eval where the string to be evaluated must be a template string expression.
>>>> 
>>>> Thomas
>>>> 
>>>> * I'm aware that someone could still put something inside a template string and do nasty stuff, but I'm not sure if that's a easily solved problem.
>>>> 
>>>>> On 13 Sep 2015, at 10:08 PM, Mark S. Miller <erights at google.com> wrote:
>>>>> 
>>>>>> On Sun, Sep 13, 2015 at 2:42 AM, Thomas <thomasjamesfoster at bigpond.com> wrote:
>>>>>> I'd really like to use Template strings as a templating language, but unless I include a lot of boilerplate code (export a template string wrapped in a function from a file)
>>>>> 
>>>>> Hi Thomas, could you give a concrete example of the boilerplate you have in mind and what it accomplishes?
>>>>> 
>>>>>  
>>>>>> or use eval after loading a file as a string it's pretty much impossible.
>>>>>> 
>>>>>> Is there a simpler way to be doing this? Or any plans for a type of eval that only executes it's argument as a template string?
>>>>> 
>>>>> I am unaware of any such plans. Could you give an example of what it looks like and what it would accomplish? Thanks.
>>>>>  
>>>>> 
>>>>> -- 
>>>>>     Cheers,
>>>>>     --MarkM
>>> 
>>> 
>>> 
>>> -- 
>>>     Cheers,
>>>     --MarkM
> 
> 
> 
> -- 
>     Cheers,
>     --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150914/e7e9cd57/attachment.html>


More information about the es-discuss mailing list