How To Enable Debugging Of Twig When Used With Slim 3 Framework

June 28, 2017

Background

When I'm developing a web application with Laravel, it has a useful helper function: dd($some_variable). It dumps the content of $some_variable into a nice and readable format. But sometimes using Laravel is overkill when the application is such a tiny to small one. Let's use this client requirement for example:

Each user has ability to use our calculator by entering some values in a form. The backend will then process those values with third-party API provider A and third-party API provider B. Finally it send the final result (after applying our formula) back to user.

Optionally send calculation result to user email.

Deadline would be 5 days after job awarded.

It's easy to develop this project using Laravel, but doesn't it feels like overkill? That's why I use Slim 3 for this case.

Enable Debugging on Slim 3 Framework

Mentioned on their documentation, Twig has a helper function called dump(). Detailed documentation available here: https://twig.sensiolabs.org/doc/2.x/functions/dump.html.

However, it's not enabled by default. We must add the Twig_Extension_Debug extension explicitly when creating our Twig environment: