Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将变量传递到嵌套的把手模板/部件中_Javascript_Handlebars.js - Fatal编程技术网

Javascript 将变量传递到嵌套的把手模板/部件中

Javascript 将变量传递到嵌套的把手模板/部件中,javascript,handlebars.js,Javascript,Handlebars.js,我有这样的背景: { path: "/some/path/to/files/", sections: [ { title: "Title 1", files: [ { name: "file1.zip" }, { name: "file2.zip" } ] } ] } 我的模板和部分: <!-- Global container template --> <script id="c

我有这样的背景:

{
  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>

认为它会将
路径
值向下传递到partials中,但它没有。我错过了什么


一个相关的问题,如果我在JavaScript中的某个地方声明了一个随机变量,那么我如何在模板和部分中访问该JavaScript变量?

您的思路是正确的,但路径需要确定

{{>文件部分路径=../path}


这是一把小提琴

啊,明白了。非常感谢。
...
{{> sectionPartial path=path }}
...
...
{{> filesPartial path=path }}
...