Search code examples
javascriptjtemplates

Reference object property 'field1' using index from {#for}


Is it possible to refer to n object property in a jTemplate {#if} by dynamically creating the property name using the index from a {#for} loop?

Example:

{#for i = 1 to 5} 
    <tr>
        <td>
           <input type="textbox" id="thisbox_{$T.i}" value="{#if $T.log.field{$T.i} != null}...{#/if}"/>
        </td>
    </tr>
{#/for}

How can I to refer to property $T.log.field# where # is the current index from the {#for}? It obviously fails when it attempts to parse {$T.log.field{$T.i}} but I'm wondering how this can be written to work or if it's even possible.


Solution

  • Answered my own question:

    I managed to fix this by referring to the property using reflection. I changed my {#if} statement to this:

    {#if $T.log['field' + $T.i] != null} ... some code here {#/if}
    

    Referring to the field by using ['field' + $T.i] worked as expected.