树枝-为什么不允许我们设置对象/数组值? [英] Twig - Why does it not allow us to set object / array values?
问题描述
Twig决定不允许通过 set
设置数组和对象属性的值,这让我感到非常困惑.
I'm extremely confused by the decision of Twig to not allow setting values of arrays and object properties via set
.
例如,以下代码将出错:
For example, the following code will error out:
{%set entry.depth = 1%}
将导致错误:
值"的意外标记标点".(预计为语句块结尾")
以下方法也会出错(我知道树枝不喜欢使用):
Also the following way will also error (which I know twig doesn't prefer to use):
{%set entry ['depth'] = 1%}
因此,这实际上意味着我们无法更改对象和数组的属性.坦率地说,我很奇怪.
So this effectively means we're unable to change properties of objects and arrays. I quite frankly find this bizarre.
有人可以解释一下背后的决定吗?也许,如果我有技术上的理由无法做到这一点,那可能会使它感到莫名其妙.
Can someone please explain the decision behind this? Maybe if I get a technical reason why it's not possible it might make it less baffling.
感谢您的解决方案,我更想知道您必须使用合并而不是简单地能够覆盖变量这一事实.
Thanks for the solution, I was more after the reasoning behind the fact you have to use merge rather than just simply being able to override variables.
推荐答案
在这方面,Twig有点奇怪.您需要为此使用 merge
过滤器.
Twig's a bit weird in this regard. You'll need to use the merge
filter for this.
{% set entry = entry|merge({'depth': 1}) %}
这篇关于树枝-为什么不允许我们设置对象/数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!