简体   繁体   中英

Error empty value when convert code from php to twig format

I have a sample code php

<?php 
echo isset($dealer_special[$dealer['customer_group_id']][$type['product_type_id']]['percent_sale']) ? $dealer_special[$dealer['customer_group_id']][$type['product_type_id']]['percent_sale'] : ''; 
?>

Convert to twig

{{ dealer_special.dealer.customer_group_id.type.product_type_id.percent_sale ? dealer_special.dealer.customer_group_id.type.product_type_id.percent_sale : '' }}

How to fix it

You didn't nest that correctly:

{{ dealer_special[dealer.customer_group_id][type.product_type_id].percent_sale|default }}

Instead you should split this to separate variables so it's more verbose and easier to comprehend and maintain:

{% set customerGroup = dealer.customer_group_id %}
{% set productTypeId = type.product_type_id %}
{% set dealerSpecial = dealer_special[customerGroup][productTypeId] %}
{{ dealerSpecial.percentSale }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM