[英]Object variable don't display in a twig template
I find this strange behaviour when using Twig (v1.23.3) 我在使用Twig(v1.23.3)时发现了这种奇怪的行为
In a templace I have an array of objects : I use the dump function to display it, like this : 在一个templace中,我有一个对象数组:我使用dump函数来显示它,如下所示:
<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. 但是for循环的输出不正确。 {{ val.name }} is displayed, but {{ val.balance }} is empty.
显示{{val.name}},但{{val.balance}}为空。
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 ! 更让人好奇的是,如果我颠倒了对象中var的顺序(这很重要),则将balance放在name之前,然后显示balance而不是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. 我想我发现了一个错误,类似于在Twig模板中未替换的变量,但是很难分辨。
That's not a bug ! 那不是臭虫! I finally enable the cache in Twig and looked at the resulting code.
最后,我在Twig中启用了缓存,并查看了生成的代码。 Guess what ?
你猜怎么了 ? Some non-printable char has find its way near the val.balance.
一些不可打印的字符已在val.balance附近找到方法。 A "cat -A" on the template file shows this :
模板文件上的“ cat -A”显示如下:
{{ 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 ? 注意到val之前的空格?
Lost one hour with this one. 失去了一个小时。 Source problem is an obsolete frenck keymap in Debian....
源问题是Debian中过时的frenck键盘映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.