Access parent variable from nested block in JsRender

前端 未结 1 1593
花落未央
花落未央 2021-01-20 02:01

How can I access props\'s key from a nested for?

{{props object.items}}
    {{:key}}
    {{for prop.other_items}}
             


        
相关标签:
1条回答
  • 2021-01-20 02:22

    Here are three alternative ways:

    Provide the key as a contextual template variable, so it is available in the {{for}} block:

    {{props object.items}}
        {{:key}}
        {{for prop.other_items ~outerKey=key}}
            Outer key: {{:~outerKey}}
       {{/for}}
    {{/props}}
    

    Provide the data item of the {{props}} block (the {key: ..., prop: ...} object) as a contextual template variable, so it is available in the {{for}} block:

    {{props object.items itemVar="~outerProp"}}
        {{:key}}
        {{for prop.other_items}}
            Outer key: {{:~outerProp.key}}
        {{/for}}
    {{/props}}
    

    Step up through the parent views (array view, then props item view) and get the data item (the {key: ..., prop: ...} object):

    {{props object.items}}
        {{:key}}
        {{for prop.other_items}}
            Outer key: {{:#parent.parent.data.key}}
        {{/for}}
    {{/props}}
    

    And here is a link to a related reply to a previous question from Matias: https://stackoverflow.com/a/31362057/1054484

    0 讨论(0)
提交回复
热议问题