What is causing a “Maximum function nesting level” error in Symfony 2.1 and Twig?

后端 未结 5 1949
滥情空心
滥情空心 2020-12-09 08:51

I have a project on Symfony 2.1. After updating composer components (Gedemo, Symfony core, Doctrine, Twig, etc..) I have the following error:

Fatal error: Ma         


        
5条回答
  •  一整个雨季
    2020-12-09 09:07

    Just sharing a little tips to new Twig developers who might be interested to understand the "What is causing.." part of the question.

    Obvisouly in the original question the max nesting level is rather low (100) and as some comments mention, it might be too low for normal circumstances.

    However, if one increases the level to, say 256, 512 or even 1000 as adviced above and still hit the same error, then perhaps the most potential thing to look at would be the template inheritance. (The extends keywords on the first line of the templates)

    This is especially the case with projects where you have templates in multiple locations.

    Imagine an example project structure:

    ── plugins
       ├── your-plugin
       |   ├── views
       |   │   ├── base.twig
       │   │   ├── special-element.twig
       │   │   ├── some-other-element.twig
    ── theme
       ├── base.twig
       ├── index.twig
       ├── sub-page.twig
    

    The plugin has a template base.twig which extends the base.twig under the theme. But if the template locations are not correctly configured, the templates might end up extending itself again and again causing the infinite loop.

    How to check if this is the case? I'd be happy to hear about more accurate solutions, but one can start - for debugging purposes only - as simple as referring to the parent templates with the full server paths:

    {% extends "/var/www/path-to-your-template/" %}

    If it starts to work with absolute paths, then you can be quite sure there is something wrong with the template paths. Read more about it here: Twig Template Naming and Locations

提交回复
热议问题