Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for "js tagged string like" feature #1987

Open
dam0vm3nt opened this issue Aug 27, 2017 · 5 comments
Open

Add support for "js tagged string like" feature #1987

dam0vm3nt opened this issue Aug 27, 2017 · 5 comments
Labels
request Requests to resolve a particular developer problem

Comments

@dam0vm3nt
Copy link

ES6 tagged string are a powerfull feature that can be leveraged to easly build wonderfull things like template engines.

@justinfagnani
Copy link

Hey, author of lit-html and former Dart team member here :)

The technique behind lit-html is already proving to be extremely fast and lightweight for HTML templating, and now there's a proposal (that implementers are very interested in) being discussed to add something like the internals of lit-html directly to the web platform: https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Template-Instantiation.md

It's really nice to be able to write HTML templates in your programming language where you have direct access to data, can use first-class language constructs, and in a way that doesn't require any reflection. I've been thinking about how to do something similar in Dart, but it really does hinge on some specifics of tagged template literals that are not possible in Dart currently.

The key feature of tagged template literals for this use case is that they separate the literal parts from expression results, and pass the literals as a single, immutable argument that's guaranteed to be referentially identical across evaluations of the tagged literal.

lit-html.dart:

Map<TemplateLiteralStrings, Template> _templateCache;

TemplateResult html(TemplateLiteralStrings strings, List values) {
  Template template = _templateCache[strings];
  if (template == null) {
    template = _makeTemplate(strings);
    _templateCache[strings] = template;
  }
  return new TemplateResult(template, values);
}

User code:

TemplateResult nameTemplate(name) => html`<div>$name</div>`;

// first render
// This passes TemplateLiteralStrings(["<div>", "</div>"]) and ["Kasper"] to html()
render(nameTemplate('Kasper'), window.document.body);

// efficient re-render
// This passes TemplateLiteralStrings(["<div>", "</div>"]) and ["Dan"] to html()
// the first argument to html is identical to the previous call
render(nameTemplate('Dan'), window.document.body);

This feature turns out to be really useful for all kinds of DSLs that are used in cases where the embedded snippet has to be evaluated repeatedly. I've seen similar techniques used in GraphQL tags that parse the GraphQS query once, the let it be re-evaluated efficiently with different query parameters.

/cc @kasperl

@dam0vm3nt
Copy link
Author

Thank you @justinfagnani !
html-lit is a super promising and interesting approach to HTML Templating and of course it's going to play an important role in the next polymer major release).

That's why I'd like to support it in polymerize but to do that definitively ES6 tagged string interpolation should be supported in dart, and that's should be no such a big problem IMHO : with a little effort there could be a great advantage for the dart lang.

/cc @jakemac53 @jmesserly @vsmenon

@knownasilya
Copy link

Agreed that this is important for JS interop.

@cah4a
Copy link

cah4a commented Dec 7, 2019

I'm the maintainer of gettext package.
I am working on a complete gettext ecosystem for dart and stumble upon interpolation.

For now, I have the only option is to separate the original string and placeholders and interpolate them only after I get the translated string. Something like:

gettext("Hello, %s", [userName])

Developers often make the same mistake, and habitually write:

gettext("Hello, ${userName}") 

And in that case, localization will not work because inside gettext function, there is no option to find out the original string.

With tagged templates support, it will be possible to make gettext as it should be: simple and intuitive.

@munificent munificent transferred this issue from dart-lang/sdk Nov 23, 2021
@munificent munificent added the request Requests to resolve a particular developer problem label Nov 23, 2021
@munificent
Copy link
Member

See #1988.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

5 participants