{"id":2263,"date":"2019-12-30T16:11:47","date_gmt":"2019-12-30T15:11:47","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/uncategorized\/content-and-templates-in-html\/"},"modified":"2020-12-16T16:20:27","modified_gmt":"2020-12-16T15:20:27","slug":"content-and-templates-in-html","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/","title":{"rendered":"Content and templates in HTML"},"content":{"rendered":"<p>Creating HTML <span id=\"urn:enhancement-e96a939b\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/web_template_system\">templates<\/span> in the context of Web programming is a very common task on both the <span id=\"urn:enhancement-c86a1a53\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/server_computing\">server<\/span> and <span id=\"urn:enhancement-bd3708a\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/client_computing\">client<\/span> sides. Traditionally, on the <span id=\"urn:enhancement-f793f62e\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/client_computing\">client<\/span> side, this task is assigned to libraries such as <a href=\"https:\/\/handlebarsjs.com\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">handlebars<\/a> or <a href=\"https:\/\/mustache.github.io\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">mustache<\/a> and similar. These libraries have defined a sort of standard in frontend programming. However, their internal behavior requires <span id=\"urn:enhancement-a2ec8686\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/string_interpolation\">string interpolation<\/span>, <span id=\"urn:enhancement-6fd21b72\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span> building and rendering, and related work that can be avoided by adopting a native approach to templating.<\/p>\n<p>A few years ago, <a href=\"https:\/\/html.spec.whatwg.org\/multipage\/scripting.html#the-template-element\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">HTML specifications<\/a> introduced a native approach to creating <span id=\"urn:enhancement-30208c16\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/web_template_system\">templates<\/span> on the <span id=\"urn:enhancement-986a9db2\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/client_computing\">client<\/span> side based on the <em>&lt;template&gt;<\/em> element. However, this native approach seems not to be well known among frontend developers. Let\u2019s take a closer look to understand how to use this approach and the benefits it brings to the table.<\/p>\n<h2 id=\"esgif\"><strong>Detecting template support<\/strong><\/h2>\n<p>Although <a href=\"https:\/\/caniuse.com\/#feat=template\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">most recent browsers widely support<\/a> the <em>&lt;template&gt;<\/em> element, it is always good practice to check whether the <span id=\"urn:enhancement-31953bb0\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/web_browser\">browser<\/span> you are using supports it. The typical approach is to create an instance of the element via <span id=\"urn:enhancement-c0cae78c\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span> <span id=\"urn:enhancement-e23c35bb\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/application_programming_interface\">APIs<\/span> and check if the content property is available, as shown in the following code:<\/p>\n<pre>if ('content' in document.createElement('template')) {<br \/>  \/\/...<br \/>}<\/pre>\n<p>If the <em>&lt;template&gt;<\/em> element is not supported, you can use your preferred templating library as a fallback.<\/p>\n<h2 id=\"48gee\"><strong>Declaring a template<\/strong><\/h2>\n<p>The <em>&lt;template&gt;<\/em> element allows us to declare a template, i.e., a structure of <span id=\"urn:enhancement-d8bec5cb\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/html_element\">HTML tags<\/span> we will fill\u00a0 with actual values at runtime. Defining a template is very easy. All you have to do is add a <em>&lt;template&gt;<\/em> element in the head or body of your HTML page and include some <span id=\"urn:enhancement-75a2b859\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/html\">HTML<\/span> in the starting and ending tags. The following is an example of template:<\/p>\n<pre>&lt;template id=\"bookDetailTemplate\"&gt;<br \/>  &lt;li&gt;<br \/>    &lt;img src=\"\"&gt;<br \/>    &lt;h5&gt;&lt;\/h5&gt;<br \/>    &lt;p&gt;&lt;\/p&gt;<br \/>  &lt;\/li&gt;<br \/>&lt;\/template&gt;<\/pre>\n<p>This example defines a list item as the template for the <span id=\"urn:enhancement-7814437a\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/markup_language\">markup<\/span> that describes a book. The details of the book description consist of an image (<em>&lt;img&gt;<\/em>), a title (<em>&lt;h5&gt;<\/em>), and a text (<em>&lt;p&gt;<\/em>). Notice that an <em>id<\/em> has been assigned to the template: <em>bookDetailTemplate<\/em>. This <em>id<\/em> is needed to identify the template when we use it at runtime.<\/p>\n<p>You can put the template element in the head section, or in the body of the HTML page &#8211; whichever is more convenient. In addition, the template element can contain any valid <span id=\"urn:enhancement-369840b7\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/html_element\">HTML markup<\/span>, including <em>&lt;style&gt;<\/em> and <em>&lt;<span id=\"urn:enhancement-995f86e6\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/scripting_language\">script<\/span>&gt;<\/em> elements, and even the <em>&lt;template&gt;<\/em> element itself. However, the markup doesn\u2019t need to represent a complete and working item. For instance, the template we defined above is not a complete list, but simply a list item. Also, the image and the other elements inside the template are not defined. Their value will be assigned at runtime, when the template will become a living branch of the <span id=\"urn:enhancement-2dcc691a\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM tree<\/span>.<\/p>\n<p>The main <span id=\"urn:enhancement-deff510e\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/software_feature\">feature<\/span> of the <em>&lt;template&gt;<\/em> element is its <em>inertness<\/em>. In fact, its content is hidden until the template is activated. Any content inside it is not loaded, nor rendered, nor <span id=\"urn:enhancement-8d6c608c\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/execution_computing\">executed<\/span>. This means that any image contained in a template is not loaded, and no <span id=\"urn:enhancement-4e220a66\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/scripting_language\">script<\/span> is <span id=\"urn:enhancement-45be2825\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/execution_computing\">executed<\/span> when the template\u2019s content is parsed. The <span id=\"urn:enhancement-5a55493\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/web_browser\">browser<\/span> only checks the markup validity and creates a hidden DOM for it while the template is not in use. All of these elements become active only when the template is used. But how does that happen?<\/p>\n<h2 id=\"7ephf\"><strong>Using a template<\/strong><\/h2>\n<p>Once a template is declared, you can use it with <span id=\"urn:enhancement-c23785f8\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/javascript\">JavaScript<\/span> in a few simple steps. First of all, you need to access the <em>&lt;template&gt;<\/em> element through the classic <em>getElementById()<\/em> method shown below:<\/p>\n<pre>const bookDetailTemplate = document.getElementById(\"bookDetailTemplate\").content;<\/pre>\n<p>In this example, the internal content of the <em>&lt;template&gt;<\/em> element is actually identified by the <em>bookDetailTemplate<\/em> id\u0010. The <em>content<\/em> property provides the <em>inert<\/em> <span id=\"urn:enhancement-ccc80a3f\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span> structure generated by the <span id=\"urn:enhancement-61fa123a\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/markup_language\">markup<\/span> inside the <em>&lt;template&gt;<\/em> element.<\/p>\n<p>Once you have access to the template\u2019s content, you can manipulate its <span id=\"urn:enhancement-4e383367\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span> structure like any standard <span id=\"urn:enhancement-e52a852\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span>. For example, you can access the image in our template with the following code:<\/p>\n<pre><span id=\"urn:enhancement-96e853f7\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/const_computer_programming\">const<\/span> img = bookDetailTemplate.querySelector(\"img\");<br \/>img.src = \"images\/myImage.jpg\";<\/pre>\n<p>However, none of this comes to life until the template\u2019s content is appended to the page\u2019s <span id=\"urn:enhancement-bb5d62fd\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span>, as in the following example:<\/p>\n<pre><span id=\"urn:enhancement-8833adf7\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/const_computer_programming\">const<\/span> bookList = document.getElementById(\"bookList\");<br \/>bookList.appendChild(bookDetailTemplate.cloneNode(true));<\/pre>\n<p>Looking at the code above, you\u2019reundoubtedly wondering why we have cloned the template\u2019s content before appending it to the <span id=\"urn:enhancement-2b9ac8ab\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span>. The answer is pretty simple; because you created a template, it&#8217;s very likely you would create multiple instances of <span id=\"urn:enhancement-9d6173e6\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span> portions from it. If, on the other hand, you don\u2019t clone the piece you are adding to the <span id=\"urn:enhancement-9ac531c3\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span>, your manipulations will affect all the other instances you\u2019ve already added. So this step is fundamental.<\/p>\n<p>To illustrate a complete example of template use at runtime, consider the following code:<\/p>\n<pre><span id=\"urn:enhancement-2c591999\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/const_computer_programming\">const<\/span> bookData = [<br \/>  {<br \/>    cover: \"...\",<br \/>    title: \"...\",<br \/>    text: \"...\"<br \/>  },  <br \/>  ...<br \/>];<br \/><span id=\"urn:enhancement-941566a5\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/const_computer_programming\">const<\/span> bookDetailTemplate = document.getElementById(\"bookDetailTemplate\").content;<br \/><span id=\"urn:enhancement-fd9a0195\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/const_computer_programming\">const<\/span> bookList = document.getElementById(\"bookList\");<br \/><br \/>for (let bookDetail of bookData) {<br \/>  <span id=\"urn:enhancement-4acffef9\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/const_computer_programming\">const<\/span> img = bookDetailTemplate.querySelector(\"img\");<br \/>  img.src = bookDetail.cover;<br \/><br \/>  <span id=\"urn:enhancement-76280880\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/const_computer_programming\">const<\/span> title = bookDetailTemplate.querySelector(\"h5\");<br \/>  title.innerText = bookDetail.title;<br \/><br \/>  <span id=\"urn:enhancement-9818f238\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/const_computer_programming\">const<\/span> text = bookDetailTemplate.querySelector(\"p\");<br \/>  text.innerHTML = bookDetail.text;<br \/><br \/>  bookList.appendChild(bookDetailTemplate.cloneNode(true));<br \/>}<\/pre>\n<p>In this example, we are mapping an array of objects, <em>bookData<\/em>, to the <span id=\"urn:enhancement-f2ad4a3c\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/document_object_model\">DOM<\/span> by using the template declared before. Notice how we create an <span id=\"urn:enhancement-b5373c46\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/instance_computer_science\">instance<\/span> of a list item for each object using the template.<\/p>\n<h2 id=\"9dfuv\"><strong>Summary<\/strong><\/h2>\n<p>As you\u2019ve seen in this article, creating and using an <span id=\"urn:enhancement-24345e01\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/html\">HTML<\/span> template via the native <em>&lt;template&gt;<\/em> element is straightforward. The declaration and use of the template are not so different from the approach suggested by most common templating libraries. However, the native <em>&lt;template&gt;<\/em> element provides you with a few benefits. It allows you to leverage the native templating engine supported by most recent browsers with noticeable performance gain. In addition, it frees users from third party library dependency.<\/p>\n<p><!-- strchf script --><script>        if(window.strchfSettings === undefined) window.strchfSettings = {};    window.strchfSettings.stats = {url: \"https:\/\/codemotion.storychief.io\/content-and-templates-in-html?id=1164399112&type=2\",title: \"Content and templates in HTML\",id: \"66543c6c-96d5-4792-a44b-49339654b7a1\"};            (function(d, s, id) {      var js, sjs = d.getElementsByTagName(s)[0];      if (d.getElementById(id)) {window.strchf.update(); return;}      js = d.createElement(s); js.id = id;      js.src = \"https:\/\/d37oebn0w9ir6a.cloudfront.net\/scripts\/v0\/strchf.js\";      js.async = true;      sjs.parentNode.insertBefore(js, sjs);    }(document, 'script', 'storychief-jssdk'))    <\/script><!-- End strchf script --><\/p>\n\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>Creating HTML templates in the context of front-end development is a very common task.Traditionally, this task is assigned to libraries, but HTML specifications have now introduced a native way to create templates  based on the <template> element. However, this native approach seems to be not well known among frontend developers. Let\u2019s take a look at it in order to understand how to use it and which benefits it brings to the table.<\/p>\n","protected":false},"author":76,"featured_media":2264,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[31],"tags":[63],"collections":[],"class_list":{"0":"post-2263","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-web-developer","8":"tag-html","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Content and templates in HTML - Codemotion Magazine<\/title>\n<meta name=\"description\" content=\"Learn how to create your HTML template using a native approach to build dynamically structured content on your Web pages.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Content and templates in HTML\" \/>\n<meta property=\"og:description\" content=\"Learn how to create your HTML template using a native approach to build dynamically structured content on your Web pages.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/\" \/>\n<meta property=\"og:site_name\" content=\"Codemotion Magazine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Codemotion.Italy\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-30T15:11:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-16T15:20:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1015\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrea Chiarelli\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:site\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrea Chiarelli\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/\"},\"author\":{\"name\":\"Andrea Chiarelli\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/6636c3b8f0c7f2f137f173229d6579d4\"},\"headline\":\"Content and templates in HTML\",\"datePublished\":\"2019-12-30T15:11:47+00:00\",\"dateModified\":\"2020-12-16T15:20:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/\"},\"wordCount\":906,\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg\",\"keywords\":[\"HTML\"],\"articleSection\":[\"Web Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/\",\"name\":\"Content and templates in HTML - Codemotion Magazine\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg\",\"datePublished\":\"2019-12-30T15:11:47+00:00\",\"dateModified\":\"2020-12-16T15:20:27+00:00\",\"description\":\"Learn how to create your HTML template using a native approach to build dynamically structured content on your Web pages.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#primaryimage\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg\",\"width\":1015,\"height\":675,\"caption\":\"Content and templates in HTML\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Frontend\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Web Developer\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Content and templates in HTML\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"name\":\"Codemotion Magazine\",\"description\":\"We code the future. Together\",\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\",\"name\":\"Codemotion\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"width\":225,\"height\":225,\"caption\":\"Codemotion\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Codemotion.Italy\/\",\"https:\/\/x.com\/CodemotionIT\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/6636c3b8f0c7f2f137f173229d6579d4\",\"name\":\"Andrea Chiarelli\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6cdfc954a6d1ba697d417cece2008f4fa62dc06f2cf345e33ecf47b3bbbb6c3e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6cdfc954a6d1ba697d417cece2008f4fa62dc06f2cf345e33ecf47b3bbbb6c3e?s=96&d=mm&r=g\",\"caption\":\"Andrea Chiarelli\"},\"description\":\"I've been working as a Software Engineer and Technical Writer for over 20 years, using several languages and technologies. I contributed to a few offline and online magazines and wrote a few books. Currently, I'm working in the Technical Experience team at Auth0.\",\"sameAs\":[\"https:\/\/andreachiarelli.it\"],\"url\":\"https:\/\/www.codemotion.com\/magazine\/author\/andrea-chiarelli\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Content and templates in HTML - Codemotion Magazine","description":"Learn how to create your HTML template using a native approach to build dynamically structured content on your Web pages.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/","og_locale":"en_US","og_type":"article","og_title":"Content and templates in HTML","og_description":"Learn how to create your HTML template using a native approach to build dynamically structured content on your Web pages.","og_url":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2019-12-30T15:11:47+00:00","article_modified_time":"2020-12-16T15:20:27+00:00","og_image":[{"width":1015,"height":675,"url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg","type":"image\/jpeg"}],"author":"Andrea Chiarelli","twitter_card":"summary_large_image","twitter_creator":"@CodemotionIT","twitter_site":"@CodemotionIT","twitter_misc":{"Written by":"Andrea Chiarelli","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/"},"author":{"name":"Andrea Chiarelli","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/6636c3b8f0c7f2f137f173229d6579d4"},"headline":"Content and templates in HTML","datePublished":"2019-12-30T15:11:47+00:00","dateModified":"2020-12-16T15:20:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/"},"wordCount":906,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg","keywords":["HTML"],"articleSection":["Web Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/","url":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/","name":"Content and templates in HTML - Codemotion Magazine","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg","datePublished":"2019-12-30T15:11:47+00:00","dateModified":"2020-12-16T15:20:27+00:00","description":"Learn how to create your HTML template using a native approach to build dynamically structured content on your Web pages.","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg","width":1015,"height":675,"caption":"Content and templates in HTML"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/content-and-templates-in-html\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"Frontend","item":"https:\/\/www.codemotion.com\/magazine\/frontend\/"},{"@type":"ListItem","position":3,"name":"Web Developer","item":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/"},{"@type":"ListItem","position":4,"name":"Content and templates in HTML"}]},{"@type":"WebSite","@id":"https:\/\/www.codemotion.com\/magazine\/#website","url":"https:\/\/www.codemotion.com\/magazine\/","name":"Codemotion Magazine","description":"We code the future. Together","publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codemotion.com\/magazine\/#organization","name":"Codemotion","url":"https:\/\/www.codemotion.com\/magazine\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","width":225,"height":225,"caption":"Codemotion"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Codemotion.Italy\/","https:\/\/x.com\/CodemotionIT"]},{"@type":"Person","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/6636c3b8f0c7f2f137f173229d6579d4","name":"Andrea Chiarelli","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6cdfc954a6d1ba697d417cece2008f4fa62dc06f2cf345e33ecf47b3bbbb6c3e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6cdfc954a6d1ba697d417cece2008f4fa62dc06f2cf345e33ecf47b3bbbb6c3e?s=96&d=mm&r=g","caption":"Andrea Chiarelli"},"description":"I've been working as a Software Engineer and Technical Writer for over 20 years, using several languages and technologies. I contributed to a few offline and online magazines and wrote a few books. Currently, I'm working in the Technical Experience team at Auth0.","sameAs":["https:\/\/andreachiarelli.it"],"url":"https:\/\/www.codemotion.com\/magazine\/author\/andrea-chiarelli\/"}]}},"featured_image_src":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-600x400.jpg","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-600x600.jpg","author_info":{"display_name":"Andrea Chiarelli","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/andrea-chiarelli\/"},"uagb_featured_image_src":{"full":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg",1015,675,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-150x150.jpg",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-768x511.jpg",768,511,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg",1015,675,false],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg",1015,675,false],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg",1015,675,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000.jpg",100,67,false],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-180x128.jpg",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-896x504.jpg",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-400x225.jpg",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-600x400.jpg",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/12\/html_13b807cc0d5e21503f6bc90f4f158d87_2000-600x600.jpg",600,600,true]},"uagb_author_info":{"display_name":"Andrea Chiarelli","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/andrea-chiarelli\/"},"uagb_comment_info":0,"uagb_excerpt":"Creating HTML templates in the context of front-end development is a very common task.Traditionally, this task is assigned to libraries, but HTML specifications have now introduced a native way to create templates based on the element. However, this native approach seems to be not well known among frontend developers. Let\u2019s take a look at it&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/2263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/comments?post=2263"}],"version-history":[{"count":4,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/2263\/revisions"}],"predecessor-version":[{"id":12777,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/2263\/revisions\/12777"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/2264"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=2263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=2263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=2263"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=2263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}