Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With "mu is too short"'s advice. And a few crazy ideas I came up with an interesting approach to complex templating. *It almost works!</p> <p>So let's say I have this content (or data or view) which I want to put into a template:</p> <pre><code>var content = { title: "Black Book", girls: ["blonde", "brunette", "redhead"], digits: ['123', '456', '789'], calc: function () { return 2 + 4; } }; </code></pre> <p>And I have a template like this:</p> <pre><code>&lt;script type="text/template" id="template"&gt; &lt;h1&gt;{{title}}&lt;/h1&gt; &lt;h3&gt;{{calc}}&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="{{digits}}"&gt;{{hair}}&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/script&gt; </code></pre> <p>And the end result I want is this:</p> <pre><code> &lt;h1&gt;Black Book&lt;/h1&gt; &lt;h3&gt;6&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="123"&gt;blonde&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="456"&gt;brunette&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="789"&gt;redhead&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>The problem we'll encounter is that we have arrays (or lists) nested in our original content, and if we tried to Mustache.render(html, content) we'd end up with only one li item or a whole array within one href="" attribute. So sad...</p> <p>So he's the approach, pass through the template multiple times. The first time, pass through and replace the top level items, and adjust the template for the next pass through. The second time, adjust one of the lists, and adjust the template for the third pass through, etc for how ever many layers of content you have. Here's the new starting template:</p> <pre><code>&lt;script type="text/template" id="template"&gt; &lt;h1&gt;{{title}}&lt;/h1&gt; &lt;h3&gt;{{calc}}&lt;/h3&gt; &lt;ul&gt; {{#hair}} {{#digits}} &lt;li&gt;&lt;a href="{{digits}}"&gt;{{hair}}&lt;/a&gt;&lt;/li&gt; {{/digits}} {{/hair}} &lt;/ul&gt; &lt;/script&gt; </code></pre> <p>On the first pass through fill in the top level stuff, and change {{digits}} to {{.}}</p> <pre><code>$.each(content, function (index, value) { template2 = template.replace(/{{title}}/ig, content.title) .replace(/{{calc}}/ig, content.calc) .replace(/{{digits}}/ig, '{{.}}'); }); </code></pre> <p>Now you have this:</p> <pre><code> &lt;h1&gt;Black Book&lt;/h1&gt; &lt;h3&gt;6&lt;/h3&gt; &lt;ul&gt; {{#hair}} {{#digits}} &lt;li&gt;&lt;a href="{{.}}"&gt;{{hair}}&lt;/a&gt;&lt;/li&gt; {{/digits}} {{/hair}} &lt;/ul&gt; </code></pre> <p>On the next pass through we'll just call Mustache.render(template2, content.digits); and that should give us:</p> <pre><code> &lt;h1&gt;Black Book&lt;/h1&gt; &lt;h3&gt;6&lt;/h3&gt; &lt;ul&gt; {{#hair}} &lt;li&gt;&lt;a href="123"&gt;{{hair}}&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="456"&gt;{{hair}}&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="789"&gt;{{hair}}&lt;/a&gt;&lt;/li&gt; {{/hair}} &lt;/ul&gt; </code></pre> <p>And that's where my logic dies, haha. Any help thinking this through would rock! I'm thinking I could take out the {{hair}} block elements and just do a $.each pass through content.girls and stack .replace three times. Or I could try to start with lowest level of content, and work my way up. I dunno.</p> <p>All this basically leaves me wondering if there's some "logic-less" way for this kind of nesting or multiple pass throughs to be put into mustache does handlebars do it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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