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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?