Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating object properties in twig

Tags:

php

twig

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

An object like the following is passed to twig:

object    property1    property2 

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?

like image 870
F21 Avatar asked Oct 10 '11 23:10

F21


1 Answers

You can do it by merging objects:

{% set object = object|merge({'property1': 'somenewvalue'}) %} 
like image 192
isqua Avatar answered Oct 12 '22 21:10

isqua