将变量传递到嵌套的 Handlebars templates/partials
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
中将 path
添加到 href
之前。但它位于另一个部分的嵌套部分中。我如何访问该值?我试过这个:
...
{{> sectionPartial path=path }}
...
和
...
{{> filesPartial path=path }}
...
认为它会将 path
值向下传递到偏音中,但它没有。我在这里错过了什么?
一个相关的问题,如果我在我的 JavaScript 中的某处声明了一个随机变量,我如何在我的模板和部分中访问该 JavaScript 变量?
你走在正确的轨道上,但路径需要
{{> filesPartial path=../path}}
这里是fiddle。 https://jsfiddle.net/18fjdq2e/1/
我有这个背景:
{
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
中将 path
添加到 href
之前。但它位于另一个部分的嵌套部分中。我如何访问该值?我试过这个:
...
{{> sectionPartial path=path }}
...
和
...
{{> filesPartial path=path }}
...
认为它会将 path
值向下传递到偏音中,但它没有。我在这里错过了什么?
一个相关的问题,如果我在我的 JavaScript 中的某处声明了一个随机变量,我如何在我的模板和部分中访问该 JavaScript 变量?
你走在正确的轨道上,但路径需要
{{> filesPartial path=../path}}
这里是fiddle。 https://jsfiddle.net/18fjdq2e/1/