Server-side components in Polymer2 template

You can add child components to templates using the Component or Element API, but because PolymerTemplate uses the shadow DOM the shadow tree is rendered instead of the elements children that are in the light DOM.

This means that the template needs to have a <slot></slot> to mark the place where the light DOM elements should be rendered.

For example you could have the html template built something like this:

HTML

  1. <link rel="import" href="/bower_components/polymer/polymer-element.html">
  2. <dom-module id="component-container">
  3. <template>
  4. <div>
  5. <slot></slot>
  6. </div>
  7. </template>
  8. <script>
  9. class ComponentContainer extends Polymer.Element {
  10. static get is() {
  11. return 'component-container'
  12. }
  13. }
  14. customElements.define(ComponentContainer.is, ComponentContainer);
  15. </script>
  16. </dom-module>

and the server-side template as:

Java

  1. @Tag("component-container")
  2. @HtmlImport("/com/example/ComponentContainer.html")
  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 template the added label and button would not be visible to the user even though it can be located in the DOM. As we can see multiple components will show up in the slot when added to the template element that has an open <slot></slot>

Note
Any element added to the light dom to be shown inside a <slot> can be removed from the dom and it will work as expected and disappear from the main element.

Default content slots and named slots in templates

The <slot> tag can contain “default” content if nothing is set from the light dom.

For example the template could be built as:

HTML

  1. <template>
  2. <div style="border: 1px solid black; padding: 10px; margin: 10px;">
  3. <slot>No components added</slot>
  4. </div>
  5. </template>

This would show at start ‘No components added’ in the slot which would then be replaced by the first added child element.

Slots can also be named so that only wanted content is added there. This is done by using the name attribute <slot name="{name-here}"> for instance:

HTML

  1. <template>
  2. <h1><slot name="title"></slot></h1>
  3. <div style="border: 1px solid black; margin: 5px;padding: 5px;">
  4. <slot>No content given!</slot>
  5. </div>
  6. </template>

Now an element that could be for instance: <label slot="title">Main header</label> would not be positioned in the slot with the default “No content given!” but into the <h1><slot name="title">…​

Named slots can also be used as default by nesting them inside the main slot like:

HTML

  1. <template>
  2. <slot name="fullName">
  3. <slot name="firstName"></slot>, <slot name="lastName"></slot>
  4. </slot>
  5. </template>

This will make the slot show data for light dom slot="firstName" slot="lastName" elements if no slot="fullName" is available and when a <element slot="fullName"> is added it will “override/replace” the firstName, lastName data.

The “default” slot and named slot can contain multiple elements.

Java

  1. @Tag("name-element")
  2. @HtmlImport("/com/example/NameElement.html")
  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", "firstName");
  10. surName.setAttribute("slot", "lastName");
  11. getElement().appendChild(firstName, middleName, surName);
  12. }
  13. }

Using a PolymerTemplate as a Parent Layout

For general info about parent views, see Router Layouts and Nested Router Targets. For general information about templates, see Creating A Simple Component Using the Template API.

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

To define a parent layout that shows the actual view below a heading a and a menu, MainLayout.html could look like this:

HTML

  1. <link rel="import" href="/bower_components/polymer/polymer-element.html">
  2. <dom-module id="main-layout">
  3. <template>
  4. <h1>Site title</h1>
  5. <div class="menu">...</div>
  6. <!-- child content comes here -->
  7. <slot></slot>
  8. </template>
  9. <script>
  10. class MainLayout extends Polymer.Element {
  11. static get is() {
  12. return 'main-layout'
  13. }
  14. }
  15. customElements.define(MainLayout.is, MainLayout);
  16. </script>
  17. </dom-module>

To use the template file, you also need a basic template component class with an html import for the template file. Right now you need to also implement the RouterLayout interface:

Java

  1. @Tag("main-layout")
  2. @HtmlImport("/com/example/ComponentContainer.html")
  3. public class MainLayout extends PolymerTemplate<TemplateModel>
  4. implements RouterLayout {
  5. }
Note
The method showRouterLayoutContent(HasElement) in the RouterLayout interface has default implementation so you don’t need to write anything in addition to that. But you may reimplement it as you wish.

Now you may use MainLayout as a parent layout via @Route annotation or @ParentLayout annotation:

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. }
Note
See Router Layouts and Nested Router Targets for the details about using routing and the annotations mentioned here.