I am beginning to learn about web components and polymer. I understand that there are mechanisms to separate content from presentation which is important in dynamically presenting content. I also understand that the "content" tag helps do so by selecting what contents (say, those within certain HTML tags or ids or classes, etc.) needs to be presented. But, I feel that the use of template tag is also in separating content from presentation and so is the use of custom element tags while the presentation/rendering is completely taken care of by JavaScript.
But we have 3 different types of tags all doing the same thing: defining content in a more structured way for ease of presentation. Could we combine some of their mechanisms into fewer tags?
It would be great if an explanation could be given, of the roles of template tags, content tags and custom-element tags, in relation to the programming idioms and design patterns followed in Web Engineering.
Custom Elements,
<template>
and<content>
elements are complementary. They can be used together or separatly.In order to define the UI of your
custom-element
tag:template
tag,content
tags inside thetemplate
in order to fetch data values from the raw DOM.Example
When you (click on the button to) register the
custom-hello
element, your browser will instantiate the element inside thediv
by calling itscreatedCallback
method.Inside
createdCallback
, the HTML template is added to the Shadow DOM root.This Shadow DOM masks the original DOM but can display useful values (here: the #prefix and #name elements) via
content
tags.