Twig:显示条纹客户卡

Twig: Displaying Stripe Customer Cards

我在 Twig 中显示客户名片时遇到一些问题。我可以 PHP 使用此代码:

$card = \Stripe\Customer::retrieve($cu->cus_id)->sources->all(['object' => 'card']);

$i = 0;

foreach ($card["data"] as $cd) {

    echo $card["data"][$i]->brand . " - " . ucwords($card["data"][$i]->funding) . " Card: " . $card["data"][$i]->last4;
    if($i == 0){
        echo " (Default)";
    }
    echo "<br />";
    $i++;
}

但我需要在 Twig 模板中输出。我尝试了以下方法:

 {% for data in cards|keys %}
     {{ card["data"][loop.index].last4 }}
 {% endfor %}

但这根本没有输出任何东西。

我正在使用 Slim PHP 框架。

您可以轻松地将 PHP 转换为 Twig。

我不知道这个管道 (cards|keys) 是如何执行的,但我确定它在官方文档中有描述 here

{% set i = 0 %}

{% for cd in card.data %}
    {{ cd.brand ~ ' - ' ~ cd.funding ~ ' Card: ' ~ cd.last4 }}

    {{ i == 0 ? ' (Default)' : '' }}
    <br />

    {% set i = i + 1 %}
{% endfor %}

它没有经过测试,但应该可以。