更新树枝中的对象属性 [英] Updating object properties in twig

查看:64
本文介绍了更新树枝中的对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在树枝中更新对象的属性?

Is there a way to update an object's property in twig?

将如下所示的对象传递给树枝:

An object like the following is passed to twig:

object
   property1
   property2

我想这样更新property1:

I would like to update property1 like this:

{% set object.property1 = 'somenewvalue' %}

上面的代码不起作用,但是可以在树枝中执行类似的操作吗?如果没有,是否有办法编写扩展或宏来做到这一点?

The above code does not work, but is it possible to do something like this in twig? If not, is there a way to write an extension or macro to do this?

推荐答案

您可以通过合并对象来实现:

You can do it by merging objects:

{% set object = object|merge({'property1': 'somenewvalue'}) %}

这篇关于更新树枝中的对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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