Since Craft 3.1.29, calling Craft::dd() or die() from within a Variable class method (i.e. something like {{ craft.someModule.someVariableMethod() }} via Twig) results in a totally blank screen. Expected and prior behaviour is that the data passed to Craft::dd() is rendered to the page, before ending the PHP process.
This issue does not happen prior to v. 3.1.29 (I tested w/ 3.1.27 and 3.1.28), and is probably related to Twig being updated to 2.11 with that release (specifically, this issue seems related).
init method: Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event) {
$variable = $event->sender;
$variable->set('someModule', ModuleVariable::class);
}
);
Craft::dd('foo'){{ craft.someModule.someVariableMethod() }}
Just fixed that for the next release, thanks for reporting!
After updating to 3.1.31, I still have this issue with when dumping & exiting in twig. This is the code that triggers the blank screen:
{{ dump('string') }}
{% exit %}
Removing {% exit %} does show the dump. Could this be related to this issue?
It’s the same root issue – Twig is now pushing all output into an output buffer, and only flushing it out to the browser once it’s done rendering everything. So if you exit the request in the middle of that, nothing will be flushed out.
{% exit %} was never meant to output anything to the browser though, besides the status template (e.g. your 404 page if you call {% exit 404 %}), which appears to still be working. So even if the behavior changed recently due to the Twig update, I wouldn’t consider it to be a bug. Perhaps we can introduce a new {% dd myVariable %} tag, which calls Craft::dd() internally.
And done! The next Craft 3.2 Beta release will have a new {% dd %} tag. Unlike dump(), that will also be available when Craft isn’t running in Dev Mode.
Most helpful comment
And done! The next Craft 3.2 Beta release will have a new
{% dd %}tag. Unlikedump(), that will also be available when Craft isn’t running in Dev Mode.