Introducing Polymer Web Elements by Google

polymer

Polymer is a library created by Google that makes it easier to create elements.

Elements are reusable sections of code that makes it much easier to implement consistent, good practices. Sometimes elements are created by web standards bodies such as the W3C, but you can also make your own elements for your own projects.

In this tutorial, I’m going to show you how to use the Polymer library to build your own elements.

polymer

Download Polymer

Go to the Polymer Project download page, click the “Get Polymer” button and then click the download button.

polymer download
  • Extract the downloaded file. I found that the file didn’t have a .zip and I had to add it manually.
  • Inside the extracted files, you’ll find a /components/ folder. Move the /components/ folder to your web project.
  • Also create an /elements/ folder.

When you’re set up correctly, your folder structure will look like the image below:

polymer download

Now you need to load the webcomponents.js file. In this, example I’m going to add this to the head of my index.html file:

<script src="components/bower_components/webcomponentsjs/webcomponents.js"></script>

Creating a web element

Let’s display the most famous phrase in the coding business: “Hello world!”.

  • Create a file inside elements folder called hello-world.html and with the following code:
<link rel="import" href="path/to/polymer.html"> 
<polymer-element name="hello-world" noscript>
  <template>
    <h1>Hello world!</h1>
  </template>
</polymer-element>
  • The link tag points to a polymer dependency. Please double check the path to polymer.html is correct in your end.
  • The polymer-element tag define the name of our custom element, in this case is hello-world.
  • The noscript attribute says this element doesn’t include the script tag.

In our main HTML file (in this example it is index.html), load your new file. Make sure to insert this line after the webcomponents.js script:

<link rel="import" href="path/to/hello-world.html">

Inside the body tag add our new web element:

<hello-world></hello-world>

Open this project in a browser to see the result:

polymer web elements

Create a dynamic web element

The previous example only displayed a static message. Now, we will show you how to add custom values to your web element and get a dynamic result: “Hello a-custom-name!”

Create a file inside elements folder named hello-name.html with the following code:

<link rel="import" href="path/to/polymer.html">
<polymer-element name="hello-name" attributes="name">
  <template>
    <h1>Hello {{name}}!</h1>
  </template>
  <script>
    Polymer({
      name: "Default Name"
    });
  </script>
</polymer-element>

The polymer-element tag not only defines the name of our custom element, but it also now allows attributes. In this case our attribute is name.

The script tag loads the code to allow a default value for the attribute name.

In our main HTML file, load this new file after webcomponents.js script:

<link rel="import" href="path/to/hello-name.html">

Inside the body tag add our new web element:

<hello-name name="John Doe"></hello-name>

Open this project in a browser to see the result:

polymer web elements

Conclusion

You can create web elements to handle your repetitive code snippets. They save you time and give you a cleaner HTML structure.

Author

  • Valentin Garcia

    Valentin discovered Joomla in 2010, and since then he has considered it as the best CMS. Valentin has been coding extensions and templates for Joomla for many years and truly enjoys helping people build their own websites with Open Source tools. He lives in San Julián, Jalisco, México.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x