robert trudel
robert trudel

Reputation: 5779

Not able to feed a template

I use handlebars and i use a helper to use condition in template; I do an ajax call to a rest architecture, i receive this data

{  
 "firstName":"Paul",
 "lastName":"Smith",
 "operationType": "Achat",
 "transactionDate":"17/08/2015",
 "operationValue":3,
 "reason":"Achat nourriture Tim Horton",
 "transactionDate":"17/08/2015"
}

My template

{{#each this}}
  <tr>
    <td>{{firstName}} {{lastName}}</td>
    <td>{{transactionDate}}</td>
    <td>{{reason}}</td>
    <td>{{#ifCond operationType '==' 'Achat'}}
          {{operationValue}}
        {{else}}
         0
       {{ifCond}}
    </td>
   <td>{{#ifCond operationType '==' 'Dépôt'}}
         {{operationValue}}
       {{else}}
         0
       {{ifCond}}
  </td>
  <td>{{#ifCond operationType '==' 'Retrait'}}
        {{operationValue}}
      {{else}}
        0
     {{ifCond}}
  </td>
</tr>
{{/each}}

Condition register

Handlebars.registerHelper('ifCond', function (v1, operator, v2, options)     {

switch (operator) {
    case '==':
        return (v1 == v2) ? options.fn(this) : options.inverse(this);
    case '===':
        return (v1 === v2) ? options.fn(this) : options.inverse(this);
    case '<':
        return (v1 < v2) ? options.fn(this) : options.inverse(this);
    case '<=':
        return (v1 <= v2) ? options.fn(this) : options.inverse(this);
    case '>':
        return (v1 > v2) ? options.fn(this) : options.inverse(this);
    case '>=':
        return (v1 >= v2) ? options.fn(this) : options.inverse(this);
    case '&&':
        return (v1 && v2) ? options.fn(this) : options.inverse(this);
    case '||':
        return (v1 || v2) ? options.fn(this) : options.inverse(this);
    default:
        return options.inverse(this);
  }
});

I get this error when the data is in the template

Uncaught Error: ifCond doesn't match each

Any idea?

Upvotes: 1

Views: 107

Answers (2)

Najkin
Najkin

Reputation: 932

Your blocks are not properly closed in your template. When you open a block with {{#tag}}, there must be a matching {{/tag}}. In your code, none of the {{#ifCond}} blocks are properly closed (the slash is missing), and the toplevel {{#each}} is not closed either (you should have {{/each}} at the end of the template).

Upvotes: 3

Roumelis George
Roumelis George

Reputation: 6746

There is no operationType attribute in your data. Try adding it.

{  
 "firstName":"Paul",
 "lastName":"Smith",
 "transactionDate":"17/08/2015",
 "operationValue":3,
 "reason":"Achat nourriture Tim Horton",
 "transactionDate":"17/08/2015",
 "operationType": "Achon" 
}

Upvotes: 1

Related Questions