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 }}
...