当给定产品有两个包含特定值的自定义字段时,我有一个div要在产品详细信息页面上显示。例如,我的目录中的每个产品都有以下自定义字段
- 库存InStore
- 库存订单
当产品的InventoryOnOrder>0并且它是InventoryInStore=0。如果能为我提供任何帮助,我将非常感激!
{{#each custom_fields}}
{{#if (toLowerCase name) '===' 'inventoryonorder'}}
{{#if value '!=' 0}}
{{#if (toLowerCase name) '===' 'inventoryinstore'}}
{{#if value '===' 0}}
<div>element A</div>
{{/if}}
{{/if}}
{{/if}}
{{/if}}
{{/each}}
提前感谢你比我聪明:(干杯!
实现这一点的最佳方法不是在#each
循环中处理逻辑和呈现组件。相反,您应该使用#each
循环来迭代custom_fields
的键,并检查当前迭代是否具有inventoryonorder
或inventoryinstore
名称,如果具有,则应使用assignVar
帮助程序为每个循环设置变量,并稍后在#each
块之外处理逻辑。
我能够在我的Stencil环境中写一个工作示例,这是我的代码:
{{#each product.custom_fields}}
{{#if (toLowerCase name) "===" "inventoryonorder"}}
{{assignVar "field_inventoryOnOrder" value}}
{{/if}}
{{#if (toLowerCase name) "===" "inventoryinstore"}}
{{assignVar "field_inventoryInStore" value}}
{{/if}}
{{/each}}
{{#all (if (getVar "field_inventoryOnOrder") ">" "0") (if (getVar "field_inventoryInStore") "===" "0")}}
<div>Display custom message</div>
{{/all}}
请注意,在each
循环中,我不呈现任何内容——这个循环是纯手把,用于设置变量以便稍后进行比较。如果你还没有听说过assignVar
助手,那么它非常有用!
在each
循环处理逻辑以分配手把变量之后,我立即使用#all
助手来确保所有参数都解析为true。如果他们这样做了,那么应该呈现内容。