在 Handlebars 中使用“#each”和多个变量
Use of "#each" with multiple variables in Handlebars
当 Handelbars 中有多个#each 时如何使用 "this" "select" 值?
{{#each marqueauction}}
{{#each modelauction}}
<div class="results__container--box">
{{this.marqueauction}}
{{this.modelauction}}
</div>
{{else}}
<div class="results__container--box">
<p>Aucun résultat d'enchères n'est disponible pour ce modèle.</p>
</div>
{{/each}}
{{/each}}
编辑:我想用 mongoose 调用我的 MongoDB 数据库并在一个盒子容器中显示结果。为此,我循环遍历我的结果数组。
我的猫鼬模型:
const DemocarauctionSchema = new Schema({
objectID: {
type: Number
},
cars_getroute: {
type: String
},
gm_url: {
type: String
},
"results": { type: [{
marque: {
type: String
},
model: {
type: String
},
model_year: {
type: String
},
price_str: {
type: String
},
prince_int: {
type: Number
},
price_currency: {
type: String
},
sold: {
type: Boolean
},
auction_house: {
type: String
},
auction_country: {
type: String
},
auction_date: {
type: String
},
auction_datetime: {
type: String
},
auction_url: {
type: String
},
image_urls: {
type: String
},
price_int_eu: {
type: Number
},
}]}
}
我遍历 Express/Node 中的数组:
const [democarauctions] = result;
let marqueauction = [];
for (let i = 0; i < democarauctions.results.length; i++) {
marqueauction.push(democarauctions.results[i].marque)
}
let modelauction = [];
for (let i = 0; i < democarauctions.results.length; i++) {
modelauction.push(democarauctions.results[i].model)
}
...
我在 "res.render":
中调用结果数组
res.render(demo, {
results: democarauctions.results,
marqueauction: marqueauction,
modelauction: modelauction,
modelyearauction: modelyearauction,
etc.})
现在我想在我的 HTML 多框上为每个结果渲染,每个结果都有 marqueauction、modelauction、modelyearauction。我用车把。
最好避免使用 this
而是使用命名 block parameters :
{{#each marqueauction as |marque|}}
{{#each modelauction as |model|}}
<div class="results__container--box">
{{marque}}
{{model}}
</div>
{{else}}
<div class="results__container--box">
<p>Aucun résultat d'enchères n'est disponible pour ce modèle.</p>
</div>
{{/each}}
{{/each}}
我不确定您为什么要将原始集合拆分为多个集合,然后尝试在演示文稿中再次组合它们。直接使用结果即可。
编辑:根据您更新的问题:
{{#each results as |auction|}}
<div class="results__container--box">
{{auction.marque}}
{{auction.model}}
</div>
{{else}}
<div class="results__container--box">
<p>Aucun résultat d'enchères n'est disponible pour ce modèle.</p>
</div>
{{/each}}
当 Handelbars 中有多个#each 时如何使用 "this" "select" 值?
{{#each marqueauction}}
{{#each modelauction}}
<div class="results__container--box">
{{this.marqueauction}}
{{this.modelauction}}
</div>
{{else}}
<div class="results__container--box">
<p>Aucun résultat d'enchères n'est disponible pour ce modèle.</p>
</div>
{{/each}}
{{/each}}
编辑:我想用 mongoose 调用我的 MongoDB 数据库并在一个盒子容器中显示结果。为此,我循环遍历我的结果数组。
我的猫鼬模型:
const DemocarauctionSchema = new Schema({
objectID: {
type: Number
},
cars_getroute: {
type: String
},
gm_url: {
type: String
},
"results": { type: [{
marque: {
type: String
},
model: {
type: String
},
model_year: {
type: String
},
price_str: {
type: String
},
prince_int: {
type: Number
},
price_currency: {
type: String
},
sold: {
type: Boolean
},
auction_house: {
type: String
},
auction_country: {
type: String
},
auction_date: {
type: String
},
auction_datetime: {
type: String
},
auction_url: {
type: String
},
image_urls: {
type: String
},
price_int_eu: {
type: Number
},
}]}
}
我遍历 Express/Node 中的数组:
const [democarauctions] = result;
let marqueauction = [];
for (let i = 0; i < democarauctions.results.length; i++) {
marqueauction.push(democarauctions.results[i].marque)
}
let modelauction = [];
for (let i = 0; i < democarauctions.results.length; i++) {
modelauction.push(democarauctions.results[i].model)
}
...
我在 "res.render":
中调用结果数组res.render(demo, {
results: democarauctions.results,
marqueauction: marqueauction,
modelauction: modelauction,
modelyearauction: modelyearauction,
etc.})
现在我想在我的 HTML 多框上为每个结果渲染,每个结果都有 marqueauction、modelauction、modelyearauction。我用车把。
最好避免使用 this
而是使用命名 block parameters :
{{#each marqueauction as |marque|}}
{{#each modelauction as |model|}}
<div class="results__container--box">
{{marque}}
{{model}}
</div>
{{else}}
<div class="results__container--box">
<p>Aucun résultat d'enchères n'est disponible pour ce modèle.</p>
</div>
{{/each}}
{{/each}}
我不确定您为什么要将原始集合拆分为多个集合,然后尝试在演示文稿中再次组合它们。直接使用结果即可。
编辑:根据您更新的问题:
{{#each results as |auction|}}
<div class="results__container--box">
{{auction.marque}}
{{auction.model}}
</div>
{{else}}
<div class="results__container--box">
<p>Aucun résultat d'enchères n'est disponible pour ce modèle.</p>
</div>
{{/each}}