-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The [3.3. Conditionals and Loops using Nested Templates][] section of the specification mentions special treatment of `<template>` elements with `[directive]` and `[expression]` attributes within `<template>` elements. They're to be treated as parts of their own, represented by an `InnerTemplatePart` interface: ```ts InnerTemplatePart : NodeTemplatePart { HTMLTemplateElement template; attribute DOMString directive; } ``` This commit introduces that class, along with special treatment whenever collecting parts from an `HTMLTemplateElement` that also has a `[directive]` attribute. To demonstrate their utility, this commit includes a test case that exercises a naive implementation of an `if` conditional. As a caveat, it's worth mentioning that the specification proposal explicitly mentions the nuance surrounding looping and conditional rendering: > this approach involves the template process callback cloning template > parts along with other nodes, or let author scripts manually specify to > which element each template part belongs. This quickly becomes an > entangled mess because now we could have multiple template parts that > refer to a single DOM location or an attribute, and we have to start > dealing with multiple template parts trying to override one another even > though there is no good use case for such a behavior. > > We like the idea of supporting very basic control flow such as `if` and > `foreach` in the default template process callback but we don't think it's > a show stopper if the default template process callback didn't support > them in the initial cut. This commit does not aim to introduce a canonical implementation for conditionals or looping, but it should enable a change like that in the future. [3.3. Conditionals and Loops using Nested Templates]: https://github.com/WICG/webcomponents/blob/gh-pages/proposals/Template-Instantiation.md#33-conditionals-and-loops-using-nested-templates
- Loading branch information
1 parent
743b970
commit 8aba104
Showing
5 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {NodeTemplatePart} from './node-template-part.js' | ||
|
||
export class InnerTemplatePart extends NodeTemplatePart { | ||
constructor(public template: HTMLTemplateElement) { | ||
super(template, template.getAttribute('expression') ?? '') | ||
} | ||
|
||
get directive(): string { | ||
return this.template.getAttribute('directive') ?? '' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters