Template strings as a template language.

Alexander Jones alex at weej.com
Sun Sep 13 15:38:16 UTC 2015


Not exactly sure what you mean. But if you are you asking how

```js
let template = 'this ${foo} and that ${bar}';
// later...
let output = String.evalTemplate(template, {foo: "thing", bar: "other
thing"});
```

is different to

```js
let template = ({foo, bar}) => `this ${foo} and that ${bar}`;
// later...
let output = template({foo: "thing", bar: "other thing"});
```

then I have a couple of answers off the top of my head:
 * The value of `template` is a simple string and thus can be trivially
loaded from JSON or a file in the former case, but not the latter. Getting
the latter involves some kind of eval anyway.
 * The number of occurrences of each template parameter (e.g. `"foo"`) is
limited to once at the definition site, and once at the invocation site,
instead of twice at definition. `with` seems like a non-starter.

On 13 September 2015 at 16:04, Mark S. Miller <erights at google.com> wrote:

> How is explicitly passing a scope different from calling the function with
> a
>
>     {title, content}
>
> object?
>
>
>
> On Sun, Sep 13, 2015 at 7:24 AM, Alexander Jones <alex at weej.com> wrote:
>
>> Same place as `eval`. Arguably both should have the option of explicitly
>> passing a scope, for extra strictness, like Python's.
>>
>> On 13 September 2015 at 15:15, 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
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>>
>>
>
>
> --
>     Cheers,
>     --MarkM
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150913/6042f89f/attachment-0001.html>


More information about the es-discuss mailing list