Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Expressions shouldn't use escaping:</p> <pre><code>- if (episode[question.name][field]) foo </code></pre> <p>(Assuming proper variable setup, obviously.)</p> <hr> <p><em>Edit to respond to comment</em></p> <p>You're not checking to see if the element exists first before trying to read the field, in both the date display and the input element... While your usecase is a bit opaque still, you need to check for the element first:</p> <pre><code>- if (episode[question.name] &amp;&amp; episode[question.name][field])", </code></pre> <p>I <em>think</em> you're trying to show an input element for things without values, and display it otherwise--not quite sure. You may want something closer to this (w/o knowing what you're doing, I made a fake template w/ what I think mirrors your data--I commented the <code>"2-PT"</code> value out to test). Some minor refactoring to clean things up, but not much.</p> <pre><code>var s = [ "ol#questions", " each question in questions", " li", " ul.question", " li: h3 (#{question.name}) #{question.description}", " if question.fields", " each field, i in question.fields", " - var foo = field === 'Date' ? 'date' : ''", " p(class='#{foo}')", " if episode[question.name]", " - var field_value = episode[question.name][field] || '' ", " if field_value", " #{field_value}", " else", " input(name='episode[#{question.name}][#{field}]', class='', value='#{field_value}')", "" ].join("\n"); var locals = { "question": { "name": "M00104" }, "episode": { "M00104": { // "2-PT": "on", "Date": "2012-01-02" } }, "questions": [ { "name": "M00104", "description": "Do we have it?", "fields": ["Date", "2-PT"] }, ] } var jade = require('jade'); var fn = jade.compile(s); console.log(fn(locals)); </code></pre> <p>After tidying:</p> <pre><code>&lt;ol id="questions"&gt; &lt;li&gt; &lt;ul class="question"&gt; &lt;li&gt; &lt;h3&gt;(M00104) Do we have it?&lt;/h3&gt; &lt;/li&gt; &lt;li style="list-style: none; display: inline"&gt; &lt;p class="date"&gt;2012-01-02&lt;/p&gt; &lt;p class=""&gt;on&lt;/p&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ol&gt; </code></pre> <p>With the <code>"2-PT"</code> line commented out:</p> <pre><code>&lt;p class=""&gt;&lt;input name="episode[M00104][2-PT]" value="" class=""&gt;&lt;/p&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