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 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义