Updating object properties in twig

后端 未结 6 832
借酒劲吻你
借酒劲吻你 2020-12-29 18:30

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

An object like the following is passed to twig:

object
   property1
   property2
6条回答
  •  难免孤独
    2020-12-29 18:50

    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 -%}
    

提交回复
热议问题