<template> tags with variable interpolation for Declarative Shadow DOM
#66
Unanswered
thescientist13
asked this question in
Q&A
Replies: 2 comments 1 reply
|
One option is make a function for it, e.g. function getTemplate(countryName, flag) {
const template = document.createElement('template');
template.innerHTML = `
<div>
<h1>
Coming From Country: <span>${countryName} ${flag}<span>
</h1>
<slot name="details"></slot>
</div>
`;
}
return template;
}Or maybe use DOM somehow? Maybe attributes? 🤔 class Greeting extends HTMLElement {
constructor(countryCode, countryName) {
...
}
connectedCallback() {
if (!this.shadowRoot) {
const flag = this.COUNTRY_FLAG_MAPPER[this.countryCode] || '🌐';
template.getElementById('span.countryName').textContent(this.countryName);
template.getElementById('span.flag').textContent(flag);
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
}
} |
0 replies
|
Was re-acquainted with the Template Instantiation / Parts proposal and realized it is exactly what this is Discussion is looking for. It seems like it would be easy enough to support? Example // Template content is `<section><h1>{{name}}</h1>Email: <a href="mailto:{{email}}">{{email}}</a></section>`
shadowRoot.appendChild(template.createInstance({name: "Ryosuke Niwa", email: "rniwa@webkit.org"}));Discussion on going here - whatwg/html#2254 Looks like GitHub has a polyfill for it? 👀 I wonder through this if there is a case to enable experimental settings in the compiler for things like this? I'm not sure how far along it is, I don't think very, so could be bleeding edge at this point? 🤔 |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Coming out #63, was curious how to support instance members when using a template tag? For example, the current convention for using
<template>is to do something like this.But what if you want to do something like this?
Not sure if there is a way to imperatively interact with
template?All reactions