handlebars - is it possible to access parent context in a partial?

后端 未结 8 1451
旧巷少年郎
旧巷少年郎 2020-12-04 15:40

I\'ve got a handlebar template that loads a partial for a sub-element.

I would need to access a variable from the parent context in the calling template, from within

8条回答
  •  情深已故
    2020-12-04 15:58

    The easiest way to pass the parent context to the partial is to do the loop inside the partial. This way the parent context is passed by default and when you do the loop inside the partial the {{../variable}} convention can access the parent context.

    example fiddle here.

    The Data

    {
      color: "#000"
      items: [
        { title: "title one" },
        { title: "title two" },
      ]
    }
    

    The Template

    Parent Color: {{color}} {{> partial}}

    The Partial

    {{#each items}}
    {{title}}
    {{/each}}

提交回复
热议问题