Cms: Blank screen when calling `Craft::dd()` from a Variable method

Created on 13 Jun 2019  Â·  4Comments  Â·  Source: craftcms/cms

Description

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).

Steps to reproduce

  1. Install Craft 3.1.29 or 3.1.30
  2. Bootstrap a module (I haven't tested w/ a plugin) and add a Variable class using something like the below, in the module's init method:
    Event::on(
            CraftVariable::class,
            CraftVariable::EVENT_INIT,
            function (Event $event) {
                $variable = $event->sender;
                $variable->set('someModule', ModuleVariable::class);
            }
        );
  1. Add a method to the Variable class that calls Craft::dd('foo')
  2. Call the Variable class' method from within a Twig template, e.g. {{ craft.someModule.someVariableMethod() }}
  3. Confirm that a blank page is rendered, rather than the string "foo"

Additional info

  • Craft version: 3.1.29, 3.1.30
  • PHP version: 7.2.14
  • Database driver & version: MySQL 5.6.42
  • Plugins & versions:

image

Most helpful comment

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings