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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分