Search code examples
javascriptjqueryjquery-templates

calling a javascript function in {{if}} of jquery template


have a javascript function as follows:

function test(item) {
       var DataToBeShownOnScreen = jQuery.data(document.body, "DataTobeShown");
       var found = $.inArray(item, DataToBeShownOnScreen) > -1;
       return found;
}

and I need to call this function in my jquery template as follows:

<script id="GridTemplate" type="text/html">
     <tbody>            
         {{each Data}} 
             <tr>
               {{each $value}} 
               {{if ${test($index)} }}                   
                   <td>${$value}</td>
               {{/if}}
               {{/each}}
             </tr>
          {{/each}} 
     </tbody>
</script>

But I am getting error:

expected identifier, string or number

I want help in understanding what is the mistake i am doing in the call of function test in the {{if}} of my template.


Solution

  • I was able to resolve this by using {{if test($index)}} instead of {{if ${test($index)}}}.

    The problem I guess was ${test($index)} would output a string rather than giving true or false as bool. Removing ${} around the function meant that I actually get the bool value.