doujiang3997 2019-05-15 15:41
浏览 255
已采纳

如何使用Twig的属性函数来访问嵌套对象属性

I'm trying to use a twig variable to access attributes of another twig variable which was not working until I found the "attributes" function. Works well except in the cases where I need to access nested attributes. It doesn't work when the variable containing the attribute is actually an object + attribute. For example:

{{ attribute(object1, variable) }} where variable = name, works just fine. {{ attribute(object1, variable) }} where variable = object2.name, doesn't work. However, a hard coded test of {{ object1.object2.name }} does work.

Here's how I got to this point...I have a yaml config file that gets parsed by the controller and passes it to twig in an array named 'config'. It contains parameters to define what gets displayed by the twig template.

fields:
  - name: company.name
    label: 'ODM'
    indexView: true
    recodrdView: true
  - name: location
    label: 'Location'
    indexView: true
    recordView: true

Additionally, an array of objects (entities) gets passed to the template for rendering.

The above "fields.name" is the name of the entity property. I iterate over the array of entities and then iterate over the config.fields array to determine what to display. Here's the twig code:

{% for data in datum %}
    <tr>
    {% for field in config.fields %}
        {% if field.indexView %}
            <td>{{ attribute(data, field.name }}</td>       
        {% endif %}
    {% endfor %}
    </tr>
{% endfor %}

I get the error:

Neither the property "company.name" nor one of the methods "company.name()", "getcompany.name()"/"iscompany.name()"/"hascompany.name()" or "__call()" exist and have public access in class "App\Entity\Odm".

I suppose I could split up the field.name string with a '.' delimiter and then make the necessary number of calls to attribute, but I'm really hoping there's a more eloquent solution. I've also tried _context['data.' ~ field.name] - no luck there either.

Any ideas?

  • 写回答

3条回答 默认 最新

  • duanjian3920 2019-05-16 05:53
    关注

    To expand on Nico's answer

    You could achieve this with the following snippet:

    main.twig

    {% import 'macro.twig' as macro %}
    {{ macro.get_attribute(foo, 'bar.foobar') }}
    

    macro.twig

    {% macro get_attribute(object, attributes) %}
        {% apply spaceless %}
        {% set attributes = attributes|split('.') %}
    
        {% set value = object %}
        {% for attribute in attributes %}
            {% set value = attribute(value, attribute|trim) is defined ? attribute(value, attribute|trim) : null %}
        {% endfor %}
        {{ value }}
        {% endapply %}    
    {% endmacro %}
    

    demo


    However a side note, if you are trying to use a macro for this and store the "output" in a variable, keep in mind that a macro will return an instance of Twig_Markup. This means you can't use the value later on.

    An example:

    data

    foo:
        bar:
            foobar: 'Lorem Lipsum'
    

    main.twig

    {% set bar = macro.get_attribute(foo, 'bar') %}
    {{ bar.foobar }} {# <--- error cause `foobar` is not a member of `Twig_Markup` #}
    

    The real value of bar actually is not even an object or array, but just a string with, in this example, Array as content

    {% set bar = macro.get_attribute(foo, 'bar') %}
    {{ bar }} {# output: Array #}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器