Using Slots in Polymer Templates

Note
Use Lit templates instead
Lit templates are recommended. Polymer templates are available in the next long term supported Vaadin version (LTS), but they are deprecated.

A Polymer template cannot render child templates created using the Component or Element API, because PolymerTemplate uses the shadow DOM that renders the shadow tree, instead of the element’s children that are in the light DOM.

To overcome this, you can add a <slot> element to mark the place where the light DOM elements should be rendered.

Example: <slot> element in a JavaScript Polymer template.

js

  1. import {PolymerElement,html} from '@polymer/polymer/polymer-element.js';
  2. class ComponentContainer extends PolymerElement {
  3. static get template() {
  4. return html`
  5. <div>
  6. <slot></slot>
  7. </div>`;
  8. }
  9. static get is() {
  10. return 'component-container';
  11. }
  12. }
  13. customElements.define(ComponentContainer.is, ComponentContainer);

Example: Mapped Java template class.

Java

  1. @Tag("component-container")
  2. @JsModule("./com/example/component-container.js")
  3. public class ComponentContainer extends PolymerTemplate<TemplateModel> {
  4. public ComponentContainer() {
  5. Element label = ElementFactory.createLabel("Main layout header");
  6. Element button = ElementFactory.createButton("Click me");
  7. getElement().appendChild(label, button);
  8. }
  9. }
  • Without the <slot> tag in the JavaScript template, the label and button would not be visible to the user, even though they can be located in the DOM.

  • You can add multiple components that will display in the slot when added to a template element with an open <slot></slot>.

Note
You can remove any element from a <slot>. It will work as expected and no longer display in the main element.

Using Default Content in the <slot> Element

The <slot> tag can contain default content that is displayed only if nothing else is set from the light DOM.

Example: <slot> element with default content in a JavaScript Polymer template.

js

  1. <div style="border: 1px solid black; padding: 10px; margin: 10px;">
  2. <slot>No components added</slot>
  3. </div>
  • ‘No components added’ displays initially, and is replaced when at least one child element is added.

Naming <slot> Elements

You can name <slot> elements using the name attribute to ensure that only wanted content is added to a particular slot.

Example Using the name attribute in the <slot> element in a JavaScript Polymer template.

js

  1. <h1><slot name="title"></slot></h1>
  2. <div style="border: 1px solid black; margin: 5px;padding: 5px;">
  3. <slot>No content given!</slot>
  4. </div>
  • A <label slot="title">Main header</label> element, for example, will now NOT be positioned in the default “No content given!” slot, but rather in the <h1><slot name="title">…​ slot.

Nesting <slot> Elements

You can also nest named slots inside the main slot.

Example: Nested slot elements in a JavaScript Polymer template.

js

  1. <slot name="fullName">
  2. <slot name="firstName"></slot>, <slot name="lastName"></slot>
  3. </slot>
  • The slot shows data for the light DOM <slot="firstName"> and <slot="lastName"> elements, if no <slot="fullName"> element is available. If you add an element to the <slot="fullName"> slot, it overrides and replaces the firstName and lastName data.

Adding Multiple <slot> Elements

The default slot and any named slot can contain multiple elements.

Example: Defining multiple elements for the default slot and multiple named slots.

Java

  1. @Tag("name-element")
  2. @JsModule("./com/example/name-element.js")
  3. public class NameElement extends PolymerTemplate<TemplateModel> {
  4. public NameElement() {
  5. Element firstName = ElementFactory.createSpan("Jack");
  6. Element middleName = ElementFactory.createSpan(" James");
  7. Element surName = ElementFactory.createSpan("Christobald");
  8. firstName.setAttribute("slot", "firstName");
  9. middleName.setAttribute("slot", "middleName");
  10. surName.setAttribute("slot", "lastName");
  11. getElement().appendChild(firstName, middleName, surName);
  12. }
  13. }

Using a PolymerTemplate as a Parent Layout

A PolymerTemplate can be used as a parent layout, by using the <slot> in the position where the child view should be displayed.

Example: JavaScript Polymer template showing the actual view, MainLayout, below a heading and menu.

js

  1. import {PolymerElement,html} from '@polymer/polymer/polymer-element.js';
  2. class MainLayout extends PolymerElement {
  3. static get template() {
  4. return html`
  5. <h1>Site title</h1>
  6. <div class="menu">...</div>
  7. <!-- child content comes here -->
  8. <slot></slot>
  9. `;
  10. }
  11. static get is() {
  12. return 'main-layout'
  13. }
  14. }
  15. customElements.define(MainLayout.is, MainLayout);

To use this template file, you need a basic Java template class that is mapped to the JavaScript template file (using the @JsModule annotation) and that implements the RouterLayout interface.

Example: Mapped Java template class that imports the JavaScript template and implements RouterLayout.

Java

  1. @Tag("main-layout")
  2. @JsModule("./com/example/main-layout.js")
  3. public class MainLayout extends PolymerTemplate<TemplateModel>
  4. implements RouterLayout {
  5. }
  • The showRouterLayoutContent(HasElement) method in the RouterLayout interface has a default implementation. This makes it unnecessary to write additional code, but you can override and re-implement it, if necessary.

You can now use MainLayout as a parent layout using the @Route or @ParentLayout annotations.

Example: Using the layout parameter in the @Route annotation to mark MainLayout as the parent layout.

Java

  1. @Route(value="editor", layout=MainLayout.class)
  2. public class Editor extends Div {
  3. }
  4. @ParentLayout(MainLayout.class)
  5. public class MenuBar extends Div {
  6. }

See the following resources for related information: