Using templates and slots - Web APIs MDN
Using templates and slots
If the same layout structure is applied to the web page repeatedly, it is important to apply a template instead of repeating the same structure over and over. In the past, this method was quite possible, but now it has become very easy thanks to the HTML component. This substance and its content are not displayed in the DOM, but can be referenced by JavaScript support.
Let's see an eas y-t o-understand example:
My notch
It will be displayed on the page only when JavaScript can be used and added to DOM in the following way:
LET TEMPLATE = document. GetElementbyid ("Custom-Paragraph"); Let TemplateContent = Template;
This is a rudimentary, but you can already understand how wonderful this is.
Using templates with web components
The template is convenient for itself, but above all, it works with the web component. Define a web component that uses the template as your own shadow DOM content. for example
CustomElements. define ("my-paragraph", class extends htmlelement); ShadowRoot.< constructor() < super(); let template = document.getElementById("custom-paragraph"); let templateContent = template.content; const shadowRoot = this.attachShadow(< mode: "open" >); ShadowRoot. AppendChild (TemplateContent. Clonenode (true)); & amp; gt; & amp; amp; amp; gt;
The important thing here is that the clone of the template content designed to actually support the node. clonenode () method is added to the shadow route.
In addition, since the content is added to the shadow DOM, the component style information can be included in the template, which is encapsulated into custom substance. This does not work simply by adding it to a normal DOM.
PMy notch
Now you can apply it simply by adding it to the HTML document:
Adding flexibility with slots
This is good, but it lacks flexibility. Only once can display a fragment of words, what does it mean, at this point, it may not be more useful than a normal paragraph! We can place it in each case of a substance so that the substance can be used in a declaration format to represent another word.
The slot is identified by the attribute name, and when the substance is used in the markup, you can modify the space in the template that can be filled in any part of the markup.
Therefore, if you want to add a connector to an obvious example, you can update the paragraph contents of the template as follows:
Default word
If the content of the slot is not defined when connecting the content to the mar k-up, or if the browser does not support the slot, simply hold the spare content My Word Default.
To qualify the slot content, include an HTML construct with a slot attribute from the entity that means exactly the same as the name of the slot you want to fill. As before, this can actually be anything:
Let's understand another text!
Note: A node that can be inserted into a slot is called a node with insertion probability. When a node is embedded in a connector, we speak of it being embedded in the connector.
Here is an example that makes it easy for us to understand. If you want to play a bit more, you can find it on GitHub (you can see it in action in real time).
A more involved example
To conclude this comment, let's look at something less boring.
The following code snippet shows how to use it together with JavaScript:
- Create a substance with a named slot in the main shadow.
- By designing the substance in this way, when it is used in the document, it will look like it is based on a collection of the substance's content and the shadow root's content.
Furthermore, a container as a template must be semantically clear in its purpose when it is used, even if it is not yet reflected in it. In addition, it is highly likely that it will be added with, for example, components that disappear again when added.
By the way, there is an absolute example on the element details page (you can also see it in real time).
Creating a template with some slots
Details< font-family: "Open Sans Light", Helvetica, Arial; >Name< font-weight: bold; color: #217ac0; font-size: 120%; >h4< margin: 10px 0 -8px 0; >h4 span< background: #217ac0; padding: 2px 6px 2px 6px; >h4 span< border: 1px solid #cee9f9; border-radius: 4px; >h4 span< color: white; >Attributes< margin-left: 22px; font-size: 90%; >Attributes p amp; amp; lt;Right name ;
Description required
Features
No
- has element has a set of CSS styles that are relevant only to the part of the document it is created in.
- Supports the name attribute, forming three named slots:
- Named slots are wrapped in substances.
Creating a new element from the
Then, with the support of Element. attachShadow, we create a new custom substance with a name and attach the part of the document we created with the support of the substance above as a shadow root. Here, we literally use the same pattern as in the previous obvious case:
customElements. define("element-detail", class expands HTMLElement< constructor() < super(); const template = document.getElementById( "element-details-template", ).content; const shadowRoot = this.attachShadow(< mode: "open" >); shadowRoot. appendChild(template. cloneNode(true)); & amp; amp; gt; & amp; amp; gt;, );
Using the custom element with named slots
Now let's understand this material and put it to use in a paper:
Slot A place in a web component that the user can fill with their own layout. - Name
- The name of the slot.
Sample A mechanism for storing buyer-side content that does not need to be displayed when the page loads, but can be instantiated at runtime with JavaScript support. Note the relevant elements in this section:
- In this fragment, we have two copies of the element that use the slot attributes to reference the named slots "element name" and "description" that we placed in the shadow root.
- Of these two elements, only the first one references the named slot "attributes". In the second one, there is no link to the named slot "attributes".
- The first element links to the named slot "attributes".
Adding a final bit of style
As a final touch, we add some more CSS for the element:
DL< margin-left: 6px; >DT< color: #217ac0; font-family: Consolas, "Liberation Mono", Courier; font-size: 110%; font-weight: bold; >dd
Body
Result
Finally, let's gather all the fragments together and see what the result looks like.
Note the following about this result:
- Even if the copies of the element in the document do not use the element directly, they will be drawn with its help, since the shadow root fills them with it.
- In the drawn result, the element's content fills the named slots from the shadow root. In other words, the DOM tree of the element is compiled together with the contents of the shadow root.
- For both elements from the shadow root, the attribute header is automatically added in the position of the named slot "attributes".
- The first element specifically references the named slot "attributes" from the shadow root, so its content will be replaced with the named slot "attributes" from the shadow root.
- The second element does not specifically reference the named slot "attributes" from the shadow root, so the content of this named slot will be filled with the default content of T from the shadow root.