Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill execution in Twig after dump()?

Tags:

twig

symfony

I'm using the {{ dump(foo) }} function in Twig to debug my templates. However, if the template is throwing errors after the dump() function, you will only see Symfony's debugging page informing you of the error. You can obviously comment out the offending lines of code in the Twig template, but is there a way to kill the execution of the template immediately after so that the output of the dump() function is the last thing printed on the screen. Naively I'm thinking of something like {{ dump(foo) }} {{ die() }}. Any ideas on how you could achieve this?

like image 755
trajan Avatar asked May 01 '15 09:05

trajan


1 Answers

I don't think you should stop PHP execution inside your twig template (even though this is possible using a custom Twig extension). The result would not be what you'd expect because there is a lot more happening between rendering your template and sending it to the browser. If you simply stop execution all this will not happen any more and I'd suspect that you'll get a simple white page.

Perhaps it's a better approach to dump the variable inside the controller. Doing that will send the dump output to the web profiler toolbar which is available even on symfony's error page.

Oh and well, what about just using a comment ({# ... #}) to disable the non-working part of your template?

like image 63
Stefan Gehrig Avatar answered Nov 08 '22 11:11

Stefan Gehrig