dongping1689 2016-01-24 21:22
浏览 28
已采纳

对象变量不显示在树枝模板中

I find this strange behaviour when using Twig (v1.23.3)

In a templace I have an array of objects : I use the dump function to display it, like this :

<pre align="left" style="text-align: left; font-size: 80%">
{{ dump(current_balances) }}
</pre>
{% for key, val in current_balances %}
    {{ val.name }} : {{ val.balance | number_format(2, '.', ' ') }} €<br>
{% else %}
    Aucun solde trouvé.
{% endfor %}

The output of dump is as expected :



      array(2) {
      [0]=>
      object(Record)#20 (3) {
        ["name"]=>
        string(32) "Bank 1 - Account"
        ["balance"]=>
        string(7) "2000.00"
      }
      [1]=>
      object(Record)#21 (3) {
        ["name"]=>
        string(32) "Bank 2 - Account"
        ["balance"]=>
        string(8) "1000.00"
      }
    }

But the output of the for loop is not correct. {{ val.name }} is displayed, but {{ val.balance }} is empty.

I dit this kind of loops hundreds of time - this code is merely cut-and-paste from a template that works.

Event more curious is that, if I invert the order of the vars in the object (as is it matters), putting balance before name, then balance is displayed, and not name !

Don't really know where to look with this one. Of course, my template are rendered with controllers calling models, so this is only a little part of the code.

I think I found a bug, similar to Variables not replaced in Twig template but it's hard to tell.

  • 写回答

1条回答 默认 最新

  • dqa35710 2016-01-24 21:53
    关注

    That's not a bug ! I finally enable the cache in Twig and looked at the resulting code. Guess what ? Some non-printable char has find its way near the val.balance. A "cat -A" on the template file shows this :

    {{ val.name }} : {{M-BM- val.balance }} M-bM-^BM-,<br>$
    

    And result in this in the compiled template :

    $this->getAttribute((isset($context[" val"]) ? $context[" val"] : null)
    

    Noticed the space before val ?

    Lost one hour with this one. Source problem is an obsolete frenck keymap in Debian....

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统