[英]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
href
的path
。 但它在另一部分內部嵌套。 我該如何訪問該值? 我試過這個:
...
{{> sectionPartial path=path }}
...
和
...
{{> filesPartial path=path }}
...
認為它會將path
值傳遞給部分,但事實並非如此。 我在這里錯過了什么?
一個相關的問題,如果我在JavaScript中的某處聲明了隨機變量,我如何在模板和部分中訪問該JavaScript變量?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.