{"id":1773,"date":"2018-04-02T21:23:56","date_gmt":"2018-04-03T02:23:56","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1773"},"modified":"2019-06-03T11:18:06","modified_gmt":"2019-06-03T16:18:06","slug":"spacebars-handle-empty-data-container-iterating","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/02\/spacebars-handle-empty-data-container-iterating\/","title":{"rendered":"Spacebars: How to handle an empty data container when iterating with #each"},"content":{"rendered":"<p>In a Meteor project, when using Blaze (Spacebars) to set up iteration through a data container in an HTML template, the normal pattern is to use an &#8216;each&#8217; block. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">{{ #each dataContainer }}\r\n   ... HTML tags here ...\r\n   ... HTML tags here ...\r\n{{ \/each }}<\/pre>\n<p>An each block starts with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">#each<\/code> and ends with\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">\/each<\/code>. What it does is iterate through every row or element in the dataContainer to produce the block of HTML code for every row. For example, if you have a list of quotes in the data container and you want to output each with some HTML\/CSS formatting:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">{{ #each listOfQuotes }}\r\n   &lt;h1 class\"bold\"&gt;&lt;i&gt;{{ quote }}&lt;\/i&gt;&lt;\/h1&gt;\r\n{{ \/each }}<\/pre>\n<p>Let&#8217;s say your data container is populated with either a Mongo DB collection or MySQL query. It is possible that the container could come back empty for a particular user. In this case, we can use an {{ else }} in our each block to provide some alternate output for the case of no query results being returned. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">{{ #each listOfQuotes }}\r\n   &lt;h1 class\"bold\"&gt;&lt;i&gt;{{ quote }}&lt;\/i&gt;&lt;\/h1&gt;\r\n   {{ else }}\r\n   No quotes found. \r\n{{ \/each }}<\/pre>\n<p>This is a convenient shortcut to handling the empty container, so that we don&#8217;t have to deal with it in our Meteor.template.helper function.<\/p>\n<p>For more information about Blaze and its use of Spacebars, see:<br \/>\n<a href=\"http:\/\/blazejs.org\/guide\/introduction.html\">http:\/\/blazejs.org\/guide\/introduction.html<\/a><br \/>\n<a href=\"http:\/\/blazejs.org\/guide\/spacebars.html\">http:\/\/blazejs.org\/guide\/spacebars.html<\/a><br \/>\n<a href=\"http:\/\/blazejs.org\/api\/spacebars.html\">http:\/\/blazejs.org\/api\/spacebars.html<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a Meteor project, when using Blaze (Spacebars) to set up iteration through a data container in an HTML template, the normal pattern is to use an &#8216;each&#8217; block. For example: {{ #each dataContainer }} &#8230; HTML tags here &#8230; &#8230; HTML tags here &#8230; {{ \/each }} An each block starts with #each and &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/02\/spacebars-handle-empty-data-container-iterating\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Spacebars: How to handle an empty data container when iterating with #each<\/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":[44,117],"tags":[119,45,116,120],"class_list":["post-1773","post","type-post","status-publish","format-standard","hentry","category-javascript-language","category-meteor","tag-blaze","tag-javascript","tag-meteor","tag-spacebars"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1773","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=1773"}],"version-history":[{"count":11,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1773\/revisions"}],"predecessor-version":[{"id":1817,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1773\/revisions\/1817"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}