簡體   English   中英

將變量傳遞給嵌套的Handlebars模板/部分

[英]Pass variable into nested Handlebars templates/partials

我有這樣的背景:

{
  path: "/some/path/to/files/",
  sections: [
    {
      title: "Title 1",
      files: [
        { name: "file1.zip" },
        { name: "file2.zip" }
      ]
    }
  ]
}

我的模板和部分:

<!-- Global container template -->
<script id="container-template" type="text/x-handlebars-template">
    {{#each sections}}
        {{> sectionPartial }}
    {{/each}}
</script>

<!-- Section partial -->
<script id="section-partial" type="text/x-handlebars-template">
    <h2>{{ title }}</h2>
    <ul>
        {{#each files}}
            {{> filesPartial }}
        {{/each }}
    </ul>
</script>

<!-- Files Partial -->
<script id="files-partial" type="text/x-handlebars-template">
    <li>
        <!-- Below is where I need to use the path value -->
        <a href="{{ path }}{{ name }}>{{ name }}</a> 
    </li>
</script>

最后,我想在files-partial hrefpath 但它在另一部分內部嵌套。 我該如何訪問該值? 我試過這個:

...
{{> sectionPartial path=path }}
...

...
{{> filesPartial path=path }}
...

認為它會將path值傳遞給部分,但事實並非如此。 我在這里錯過了什么?

一個相關的問題,如果我在JavaScript中的某處聲明了隨機變量,我如何在模板和部分中訪問該JavaScript變量?

你走在正確的軌道上,但路徑需要

{{> filesPartial path=../path}}

這是一個小提琴。 https://jsfiddle.net/18fjdq2e/1/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM