对象变量不显示在树枝模板中 [英] Object variable don't display in a twig template

查看:34
本文介绍了对象变量不显示在树枝模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Twig (v1.23.3) 时发现这种奇怪的行为

在一个临时空间中,我有一个对象数组:我使用转储函数来显示它,如下所示:

{{ 转储(current_balances)}}

{% for key, val in current_balances %}{{ val.name }} :{{ val.balance |number_format(2, '.', ' ') }} €
{% 别的 %}Aucun 销售 trouvé.{% 结束为 %}

dump 的输出符合预期:

<预><代码>数组(2){[0]=>对象(记录)#20 (3) {[姓名"]=>string(32) "银行 1 - 账户"[余额"]=>字符串(7)2000.00"}[1]=>对象(记录)#21(3){[姓名"]=>string(32) "银行 2 - 账户"[余额"]=>字符串(8)1000.00"}}

但是for循环的输出不正确.显示{{ val.name }},但{{ val.balance }} 为空.

我重复了数百次这种循环 - 这段代码只是从一个有效的模板中剪切和粘贴而来.

事件更奇怪的是,如果我反转对象中变量的顺序(这很重要),将 balance 放在 name 之前,然后显示 balance,而不是 name !

真的不知道在哪里看这个.当然,我的模板是通过控制器调用模型来渲染的,所以这只是代码的一小部分.

我想我发现了一个错误,类似于 Twig 模板中未替换的变量 但很难说.

解决方案

这不是错误!我终于在 Twig 中启用了缓存并查看了生成的代码.你猜怎么着 ?一些不可打印的字符已经在 val.balance 附近找到了.模板文件上的cat -A"显示了这一点:

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

并在编译的模板中产生这个结果:

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

注意到 val 之前的空格了吗?

用这个浪费了一个小时.源问题是 Debian 中过时的 frenck 键盘映射....

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.

解决方案

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....

这篇关于对象变量不显示在树枝模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆