title: Condition in Handlebars' if statement
body: <p>I'm getting to know Handlebars.js and I would have a question for the community. I know I have a lot more to learn and I'm on my way, but I'd like to see an example to this problem.</p>
  
  <p><strong>The array created in JS with the objects:</strong></p>
  
  <pre><code>var data = 
  [
  {
      Field: "id",
      Type: "int(11)",
      Null: "NO",
      Key: "PRI",
      Default: null,
      Extra: "auto_increment"
  },
  {
      Field: "id2",
      Type: "int(131)",
      Null: "N3O",
      Key: "PR3I",
      Default: null,
      Extra: "auto_increment"
  }
  ];
  </code></pre>
  
  <p>The format is this because the JSON I receive from the server will look like the exact way, but now for testing I didn't want to make an ajax call.</p>
  
  <p><strong>The template:</strong></p>
  
  <pre><code>&lt;table&gt;
          &lt;thead&gt;
          &lt;tr&gt;
  
          {{#each this}}
             {{#only_once this}}
              {{#key_value this}}
                  &lt;th&gt;{{key}}&lt;/th&gt;      
              {{/key_value }}
              {{/only_once}}
          {{/each}}
  
          &lt;/tr&gt;    
          &lt;/thead&gt;
  ...
  </code></pre>
  
  <p>Because the objects are in an array, I have to loop firstly the array with <code>{{#each}}</code> then there comes a registered helper (I found on github) that help me get the key because I want to write only them to the thead.</p>
  
  <p>Without my if statement it works fine, fill in the thead with the keys, but <em>because there are 2 objects, it prints out the names twice</em>.</p>
  
  <p><strong>My problem is that I want to print them only once and an if would solve my problem that checks if the index of the array is greater than 0 to stop printing out the data, but..</strong></p>
  
  <p><strong>.. Handlebars doesn’t support conditional statements</strong>, so code like <code>{{#if x &gt; y}}</code> isn’t possible. What do you guys think would be the best solution for it?</p>
  
  <pre><code>Handlebars.registerHelper("only_once", function(item, fn){
      var buffer;
      var i = 0;
  
      if (i &gt; 0) {
          buffer = false;
      }
  
          i++;
  
      return buffer;
  });
  </code></pre>
  
  <p>Well, I tried to write a helper, but I think I did something wrong. My theory was that I give to my if the '<code>this</code>' in the template as it (I think) points back to the array and then increase the <code>i</code> to check if the index of the array is > than 0, finally if it's true than send back a false - so I thought it will say to the if that don't run the code inside, but I though wrongly. </p>