WICG / webcomponents

Web Components specifications
Other
4.38k stars 375 forks source link

[templates] How to handle expressions in tables #703

Open justinfagnani opened 7 years ago

justinfagnani commented 7 years ago

Currently, this won't parse in a way that's useful for dynamically creating tables:

<template>
  <table>
    {{rows}}
  </table>
</template>

because the {{rows}} text will be fostered to before the table.

A couple of options are:

jakearchibald commented 7 years ago

Parser changes can be limited to cases where the first non-whitespace chars are {{

justinfagnani commented 7 years ago

If there are parser changes, I think we might still need a syntax fallback for older browsers using a template parts polyfill.

matthewp commented 7 years ago

@justinfagnani Am I reading this right, is the idea that you can pass something like a DocumentFragment as rows to createInstance()? Meaning values are not all toStringed? If so, what types are allowed?

justinfagnani commented 7 years ago

@matthewp yes, the key type allowed besides Strings are Nodes, especially TemplateInstance form this proposal.

jakearchibald commented 7 years ago

TPAC: consensus for parser changes.

wanderview commented 7 years ago

I don't think anyone from mozilla parser team has weighed in yet. cc / @hsivonen

jakearchibald commented 7 years ago

Apologies, I just wanted to capture what happened in the room, it wasn't definitive (but I see how it can be read like that).

dy commented 4 years ago

That problem can be addressed purely symptomatically with a couple of strong heuristics.

  1. If there's insertion {{ }} right before the empty table, almost likely it was intended to be inside but parser hoisted it up.
  2. If the insertion {{ }} contains <tbody>, <tr>, <th> etc. - most surely it was supposed to reside in the <table>.

These two conditions I believe exclude practical chance user meant to place <tr> (or <thead> etc.) right before the empty <table>.

dy commented 2 years ago

As discovered in templize and uce-template, the simplest workaround is

<template>
  <table>
    <!--{{rows}}-->
  </table>
</template>

It has zero cost from performance/code size perspective.