ZhukV
ZhukV

Reputation: 3188

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

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: Maximum function nesting level of '100' reached, aborting! in /var/www/{path}/vendor/twig/twig/lib/Twig/Token.php on line 78

I have PHP 5.4. What can cause this error?

Upvotes: 30

Views: 32396

Answers (5)

Kasun
Kasun

Reputation: 689

if increasing xdebug.max_nesting_level = 1000 failed check namespace is correct

eg: namespace App\Entities; but folder name in lower case app

then entity should contain namespace as namespace app\Entities;

Upvotes: 0

jgangso
jgangso

Reputation: 658

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

Upvotes: 1

Adnen Chouibi
Adnen Chouibi

Reputation: 416

it is an error code that causes an infinite loop, but it happens from time to time that treatment without error exceeds the 100 calls nested functions.

To correct this, open the php.ini, xdebug section and add the following line (putting what you want instead of 150)

 [xdebug]
xdebug.max_nesting_level = 150

Upvotes: 2

WVillefort
WVillefort

Reputation: 1

In my case, I had to increase the amount of memory used by PHP in php.ini to 512MB. Also, I made a composer update on the site root folder to update the default settings generated by Symfony.

Upvotes: -2

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44851

Find the xdebug.ini file:

$ locate xdebug.ini
/etc/php5/conf.d/20-xdebug.ini
/etc/php5/mods-available/xdebug.ini

In my case the file is /etc/php5/conf.d/20-xdebug.ini. Open it and add this line:

xdebug.max_nesting_level = 1000

Don't forget to restart the FPM server.

Upvotes: 55

Related Questions