{"id":2422,"date":"2018-10-20T21:06:00","date_gmt":"2018-10-21T02:06:00","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2422"},"modified":"2018-11-02T19:46:37","modified_gmt":"2018-11-03T00:46:37","slug":"handlebars-how-to-use-templates","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/10\/20\/handlebars-how-to-use-templates\/","title":{"rendered":"Handlebars: How to use templates"},"content":{"rendered":"<p>Handlebars supports multiple templates in the same Single Page Application. To use templates with Handlebars, you must define a template id in the opening Handlebars script tag. For example, in this case the name of the template is &#8220;verse-set-grid&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;script id=\"verse-set-grid\" type=\"text\/x-handlebars-template\"&gt;<\/pre>\n<p>The pattern for a template is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;!-- 1. Opening script tag with the name of the template set in the id --&gt;\r\n&lt;script id=\"verse-set-grid\" type=\"text\/x-handlebars-template\"&gt;\r\n\r\n&lt;!-- 2. The body of the template where all of the Handlebars substitutions are made. --&gt;\r\n...\r\n...\r\n\r\n&lt;!-- 3. The closing script tag --&gt;\r\n&lt;\/script&gt;<\/pre>\n<p>Here is a complete template example. Inside the &lt; script &gt; tags is the handlebars template definition. Somewhere else on the page, in this case following the template definition, is the target div (called\u00a0&#8220;verse-set-grid&#8221; ). When the handlebars template is compiled it will be placed inside the specified target div.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;!-- BEGIN verse-set-grid template --&gt;\r\n&lt;script id=\"verse-set-grid\" type=\"text\/x-handlebars-template\"&gt;\r\n{{#each this}}\r\n&lt;div class=\"verse-set\" id=\"{{setID}}\"&gt;\r\n  &lt;div class=\"verse-set-header\"&gt;&lt;span&gt;{{setTitle}}&lt;\/span&gt;&lt;\/div&gt;\r\n  &lt;div class=\"verse-set-box\"&gt;\r\n\t&lt;ul&gt;\r\n\t  {{#each verses}}\r\n\t  &lt;li&gt;{{verseRef}}&lt;div class=\"view-verse-text\"&gt;{{verseText}}&lt;\/div&gt;\r\n\t  {{\/each}}\r\n\t&lt;\/ul&gt;\r\n   &lt;\/div&gt;\r\n  &lt;div class=\"play-button\"&gt;Play This Set&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n{{\/each}}\r\n&lt;\/script&gt;\r\n&lt;!-- END verse-set-grid template --&gt;\r\n\r\n&lt;!-- Target div: this is where the handlebars template will appear on the page --&gt;\r\n&lt;div class=\"verse-set-grid\"&gt;&lt;\/div&gt;<\/pre>\n<p>Now the second part of getting templates to work in Handlebars is the behind the scenes steps in JavaScript.<\/p>\n<h4><strong>Step 1: Access the template via jQuery<\/strong><\/h4>\n<p>Notice the jQuery selector here is targeting the template ID &#8220;verse-set-grid&#8221;, not the div class with the same name:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let verseSetsGrid = $('#verse-set-grid').html();<\/pre>\n<h4><strong>Step 2: Compile the template with Handlebars.compile<br \/>\n<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let verseSetsGridCompiled = Handlebars.compile(verseSetsGrid);<\/pre>\n<h4><strong>Step 3: Get the data that Handlebars will use for the template substitutions<br \/>\n<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let verseSetsData = []\r\nlet verseSetsData = JSON.parse(result)<\/pre>\n<h4><strong>Step 4: Pass the data to the compiled Handlebars object, which then places it in a new html variable<br \/>\n<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let html = verseSetsGridCompiled(verseSetsData);<\/pre>\n<h4><strong>Step 5: Use jQuery to place the updated html back into the template DOM<br \/>\n<\/strong><\/h4>\n<p>Notice the selector target here is the &#8216;.verse-set-grid&#8217; <em>class<\/em>, not <em>id<\/em>! This is the div that contains the verse-set-grid template.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$('.verse-set-grid').html(html);<\/pre>\n<p>All five steps together looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/ Step 1\r\nlet verseSetsGrid = $('#verse-set-grid').html();\r\n\/\/ Step 2\r\nlet verseSetsGridCompiled = Handlebars.compile(verseSetsGrid);\r\n\r\n\/\/ Step 3\r\nlet verseSetsData = []\r\n\/\/ This is an AJAX query that calls a PHP function to return MySQL query results\r\n$.get(cardSets, args, function(result)\r\n{\r\n   let verseSetsData = JSON.parse(result)\r\n   \/\/ Step 4\r\n   let html = verseSetsGridCompiled(verseSetsData);\r\n   \/\/ Step 5\r\n   $('.verse-set-grid').html(html);\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>For more information about Handlebars templates, see:<br \/>\n<a href=\"http:\/\/handlebarsjs.com\/\">http:\/\/handlebarsjs.com\/<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Handlebars supports multiple templates in the same Single Page Application. To use templates with Handlebars, you must define a template id in the opening Handlebars script tag. For example, in this case the name of the template is &#8220;verse-set-grid&#8221;: &lt;script id=&#8221;verse-set-grid&#8221; type=&#8221;text\/x-handlebars-template&#8221;&gt; The pattern for a template is: &lt;!&#8211; 1. Opening script tag with the &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/10\/20\/handlebars-how-to-use-templates\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Handlebars: How to use templates<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179,44],"tags":[65,180,45,123],"class_list":["post-2422","post","type-post","status-publish","format-standard","hentry","category-handlebars","category-javascript-language","tag-ajax","tag-handlebars-js","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2422"}],"version-history":[{"count":11,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2422\/revisions"}],"predecessor-version":[{"id":2436,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2422\/revisions\/2436"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}