Search code examples
jsrender

Passing a value from outer for to an inner for using jsRender


if I do the following:

{{for Items}}
{{>Id}}
   {{for messages}}
       <div class="myclass">
          {{include tmpl="#myTemplate" /}}
       </div>
   {{/for}}
{{/for}}

then at the top of my item I correctly see the value of Id

if I now remove {{>Id}} since I've proved this is what I want

I now want to add it to my div such that it becomes:

<div class="myclass" productid="{{:Id}}">

however the Id value doesn't get set against my productid attribute I've since tried {{:Items.Id}}, {{:parent.Id}} plus a few others to no avail.


Solution

  • There are several ways you can get to "parent data". See https://www.jsviews.com/#parentdata. For example you can do

    {{for messages ~parentId=Id}}
       <div class="myclass" productid="{{:~parentId}}">
          {{include tmpl="#myTemplate" /}}
       </div>
    {{/for}}
    

    or you can step up through the 'array' view of {{for messages}} and get to the parent 'item' view of {{for Items}}, using {{:#parent.parent.data.Id}}. (See https://www.jsviews.com/#views)