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

Render across <template> element boundaries #72

Merged
merged 1 commit into from
Nov 11, 2024

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Nov 3, 2024

Take, for example, a <template> that nests other <template> elements:

<template>
  <template>
    <div>{{x}}</div>
  </template>
</template>

<script type="module">
  import { TemplateInstance } from "@github/template-parts"

  const template = document.querySelector("template")
  const instance = new TemplateInstance(template, { x: "Hello world" })

  document.body.append(instance)
</script>

Prior to this change, the inner <template> element (and its child <div>) are unchanged and still render {{x}} as a text node.

This change aims to bring support for templating across <template> boundaries. To achieve this behavior, add explicit HTMLTemplateElement handling to the tree walking. When handling a <template> element, walk its content DocumentFragment, treating variables in the same as way as the outer <template> element.

Without this support, explicit iteration and replacement is necessary for each boundary:

const values = { x: "Hello world" }
const template = document.querySelector("template")
const instance = new TemplateInstance(template, values)

for (const nestedTemplate of instance.querySelectorAll("template")) {
  nestedTemplate.content.replaceChildren(new TemplateInstance(nestedTemplate, values))
}

@seanpdoyle seanpdoyle requested a review from a team as a code owner November 3, 2024 17:38
Copy link
Member

@keithamus keithamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@keithamus
Copy link
Member

@seanpdoyle the test runner is being a bit wonky, but I pulled locally (and rebased with main to get the updates) and it looks like the test is failing.

Would you kindly rebase this with main and try looking at the test failure? Thanks!

@seanpdoyle
Copy link
Contributor Author

@keithamus I faced a similar set of failures locally. I was hoping that CI would execute and give me more information.

What steps might I be missing locally to run the suite?

@keithamus
Copy link
Member

If you rebase you might find it fixed. I had the issues locally and simply updated all dependencies which got tests working again for me, so I assumed we had a bit of dependency rot.

@seanpdoyle seanpdoyle force-pushed the span-template-boundary branch 2 times, most recently from e14ddf4 to a6895d9 Compare November 8, 2024 18:47
@seanpdoyle
Copy link
Contributor Author

@keithamus thank you for the guidance. I was able to successfully pass the suite locally. Could you enable CI?

Take, for example, a `<template>` that nests other `<template>`
elements:

```html
<template>
  <template>
    <div>{{x}}</div>
  </template>
</template>

<script type="module">
  import { TemplateInstance } from "@github/template-parts"

  const template = document.querySelector("template")
  const instance = new TemplateInstance(template, { x: "Hello world" })

  document.body.append(instance)
</script>
```

Prior to this change, the inner `<template>` element (and its child
`<div>`) are unchanged and still render `{{x}}` as a text node.

This change aims to bring support for templating across `<template>`
boundaries. To achieve this behavior, add explicit
[HTMLTemplateElement][] handling to the tree walking. When handling a
`<template>` element, walk its content `DocumentFragment`, treating
variables in the same as way as the outer `<template>` element.

[HTMLTemplateElement]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement
@keithamus keithamus merged commit e4ef636 into github:main Nov 11, 2024
1 check passed
@seanpdoyle seanpdoyle deleted the span-template-boundary branch December 2, 2024 21:44
@seanpdoyle
Copy link
Contributor Author

@keithamus thank you for merging this and including it as part of the v0.6.4 release!

I'm trying to install that release from npm, but the latest available release is v0.5.4.

Is that deliberate? If not, are you able to release v0.6.4 to npm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants