indexingviewtotal.jsinline-variable

In total.js is it possible to assign value to variables or save/use the index of a @{foreach...} inside a nested @{foreach...} inline in the HTML?


for example is it possible to define/use an "item_index" in a similar example to this:

<div>
 @{foreach item in list}
  <p>@{item.title}:<p>
  @{foreach content in item.contents}
   <p>@{content.name} is the @{index}th content of @{item.title}, and the @{item_index}th item in the list</p>
  @{end}
 @{end}
</div>

is it possible to assign values like @{item_index = index} before going into the nested loop?


Solution

  • You can use R (short for repository) to store extra data. This works:

    @{foreach m in [1, 2, 3, 4]}
        @{R.index = index}
        @{foreach m2 in [10, 20, 30, 40]}
            <div>@{m2}, index: @{R.index}, index2: @{index}</div>
        @{end}
    @{end}
    

    output:

    <div>10, index: 0, index2: 0</div>
    <div>20, index: 0, index2: 1</div>
    <div>30, index: 0, index2: 2</div>
    <div>40, index: 0, index2: 3</div>
    <div>10, index: 1, index2: 0</div>
    <div>20, index: 1, index2: 1</div>
    <div>30, index: 1, index2: 2</div>
    <div>40, index: 1, index2: 3</div>
    <div>10, index: 2, index2: 0</div>
    <div>20, index: 2, index2: 1</div>
    <div>30, index: 2, index2: 2</div>
    <div>40, index: 2, index2: 3</div>
    <div>10, index: 3, index2: 0</div>
    <div>20, index: 3, index2: 1</div>
    <div>30, index: 3, index2: 2</div>
    <div>40, index: 3, index2: 3</div>