Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Solved.</p> <p>My solution was to remove the {{#each}} logic from the template and replace it with a single handlebars helper expression, and pass back the needed html all at once. The html is generated from data in the cursor of the Categories collection.</p> <p>Not so sure about having all this html in my logic though -- is this bad design? If so I'll defer to a better answer. </p> <pre><code>// This is the new template &lt;template name="content"&gt; &lt;h1&gt;Categories&lt;/h1&gt; {{listCategories}} &lt;/template&gt; // Handlebars helper Handlebars.registerHelper('listCategories', function() { var parents = Categories.find({idCategoryParent:0}); var countParents = parents.count(); var string = ''; // iterate over each parent category for(m = 0; m &lt; countParents; m++){ // Get the parents category id var pId = parents.db_objects[m].idCategory; var children = Categories.find({idCategoryParent:pId}); var count = children.count(); /* * Build the Parent Category html * Example: &lt;b&gt;Movies&lt;/b&gt;&lt;ul&gt; */ string = string + '&lt;b&gt;' + parents.db_objects[m].defaultLabel + '&lt;/b&gt;&lt;ul&gt;'; // iterate over each child category for(var i = 0; i &lt; count; i++){ /* * Build the child category html * Example: &lt;li&gt;Horror&lt;/li&gt; */ string = string + '&lt;li&gt;' + children.db_objects[i]['defaultLabel'] + '&lt;/li&gt;'; } // Close up the unordered list string = string + '&lt;/ul&gt;'; } // Return the string as raw html return new Handlebars.SafeString(string); }); // Rendered out the result correctly like so: &lt;b&gt;Movies&lt;/b&gt; &lt;ul&gt; &lt;li&gt;Horror&lt;/li&gt; &lt;li&gt;Comedy&lt;/li&gt; &lt;li&gt;Action&lt;/li&gt; &lt;li&gt;Drama&lt;/li&gt; &lt;/ul&gt; &lt;b&gt;Music&lt;/b&gt; &lt;ul&gt; &lt;li&gt;Rock&lt;/li&gt; &lt;li&gt;Classical&lt;/li&gt; &lt;li&gt;Ambient&lt;/li&gt; &lt;/ul&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload