Skip to content

Twig Template fails when a child name matches a item's method name #233

Description

@oparadis

When using the TwigRenderer, the current implementation of "knp_menu.html.twig" accesses to the methods of an MenuItem by using a property path syntax.

{{ item.label }} accesses the $item->getLabel() method.

Unfortunately, since ItemInterface extends \ArrayAccess, it is possible that a child name conflicts with a method name. Since \ArrayAccess has priority, it'll not call the intended methods.

For example, by adding a menu such as $menu->addChild('label', []);, in the twig template, {{ item.label }} will access $item['label'] instead. It will return an ItemInterface object and fail with a "object cannot be converted to a string" error.

By adding a 'children' item, you could skip part of a menu because it uses the child's children instead of the item's children.

I guess that it should be safer to use method names in the twig template,such as {{ item.getLabel() }}, to ensure that the methods are called and not children items.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions