0

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?

Garen
  • 30
  • 3

1 Answers1

1

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>
Molda
  • 5,619
  • 2
  • 23
  • 39
  • very clean solution. even though R is undefined for me but I assume it is because the project is still running on (v2.x.x). however, until it's upgraded to (v3.x.x) replacing R with F seems to be doing the trick. Also note that the assignment statement @{F.list_index = index} itself is printing out the index so i had to hide that with adding a style 'hidden'. So I just want to ask if using F is a bad idea. – Garen Nov 27 '19 at 12:04
  • 1
    Well F holds every function framework has so the chances you overwrite one of them is quite high although if you only use F.index(I believe it's not used internally) you should be fine.. In this case it would be better to use repository instead of R @{repository.index = index} Also i believe the "printing out the index" is fixed in v3.x.x – Molda Nov 27 '19 at 12:58