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 had the same problem in my knp menu template. I wanted to render an alternate field with the label block, without duplicating it. Of course the underlying object needs an setter for the property.
{%- block nav_label -%}
{%- set oldLabel = item.label %}
{%- set navLabel = item.getExtra('nav_label')|default(oldLabel) %}
{{- item.setLabel(navLabel) ? '' : '' }}
{{- block('label') -}}
{{- item.setLabel(oldLabel) ? '' : '' }}
{%- endblock -%}