Using sub-template from PolymerTemplate

You can configure Java instances to be created for custom elements inside your HTML template file with the @Uses annotation.

Here is an example of parent HTML template file:

HTML

  1. <link rel="import" href="/bower_components/polymer/polymer-element.html">
  2. <link rel="import" href="/com/example/ChildTemplate.html">
  3. <dom-module id="parent-template">
  4. <template>
  5. <div>Parent Template</div>
  6. <div>[[name]]</div>
  7. <child-template>
  8. </template>
  9. <script>
  10. class ParentTemplate extends Polymer.Element {
  11. static get is() { return 'parent-template' }
  12. }
  13. customElements.define(ParentTemplate.is, ParentTemplate);
  14. </script>
  15. </dom-module>

The Java counterpart defines that ChildTemplate should be instantiated for any template element with a matching tag name (i.e. child-template).

Java

  1. @Tag("parent-template")
  2. @HtmlImport("/com/example/ParentTemplate.html")
  3. @Uses(ChildTemplate.class)
  4. public class ParentTemplate extends PolymerTemplate<Model> {
  5. }
  6. public interface Model extends TemplateModel {
  7. void setName(String name);
  8. String getName();
  9. }

The HTML template uses a child-template element, which is a custom element defined in ChildTemplate.html. Since the client-side implementation of child-template depends on a click handler defined from server-side Java, an instance of ChildTemplate must be created on the server. By using @Uses(ChildTemplate.class), this instance will automatically be created and hooked up with the <child-template> element in the parent template based on the @Tag("child-template") annotation value.

Note
Server-side instances are also created when using @Id to connect elements together. You don’t need @Uses for elements that are already covered by @Id, and vice versa.

Here is the child template Java code:

Java

  1. @Tag("child-template")
  2. @HtmlImport("/com/example/ChildTemplate.html")
  3. public class ChildTemplate extends PolymerTemplate<TemplateModel> {
  4. @EventHandler
  5. private void handleClick() {
  6. System.out.println("Click on Button in the child template");
  7. }
  8. }

And it’s the HTML template counterpart:

HTML

  1. <link rel="import" href="/bower_components/polymer/polymer-element.html">
  2. <dom-module id="child-template">
  3. <template>
  4. <button on-click="handleClick">Child Template</button>
  5. </template>
  6. <script>
  7. class ChildTemplate extends Polymer.Element {
  8. static get is() { return 'child-template' }
  9. }
  10. customElements.define(ChildTemplate.is, ChildTemplate);
  11. </script>
  12. </dom-module>

This template delegates a click handler to the server side and the method handleClick will be called on the ChildTemplate instance (not ParentTemplate instance).

Note
You can detect whether a component is part of a Template by using the isTemplateMapped method. See the Integrating components in a PolymerTemplate tutorial for more details.