假设在 Handlebars 中我们有这个 cards-list.html
部分的:
{{#each data}}
<article class="card card_list-view card_list-view--regular">
<picture class="card-image">
<img src="{{root}}/assets/img/{{this.img}}" alt="">
</picture>
<section class="card-section">
<header>
<h3><a href="{{this.url}}">{{this.title}}</a></h3>
</header>
</section>
</article>
{{/each}}
数据是这样的:
{"id": "1",
"title": "A",
"img": "imga.jpg",
"url": "card-single.html"
},
{"id": "2",
"title": "B",
"img": "imgb.jpg",
"url": "card-single.html"
}
并且 - 在
card-single.html
- 我想简单地渲染单卡:<article class="card card_single-view">
<h4>{{title}}</h4}
[etc..]
我怎样才能通过,通过
href
属性或以另一种方式,cards-list.html
的原始上下文偏向 card-single.html
?谢谢!
最佳答案
使用 Handlebars.registerPartial
创建部分后您可以将其包含在模板中,如下所示:
{{> cardSingle }}
此语法还接受对象参数:
{{> cardSingle objectOfThings }}
所以在您的
cards-list.html
你可以有类似的东西:{{#each data}}
{{> cardSingle this }}
{{/each}}
还有您的
cardSingle
部分可以使用 this
的属性直接地:<h4>{{title}}</h4>
关于handlebars.js - Handlebars : transfer information from a page to another page via href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49013740/