Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] @dump blade directive #590

Closed
kleshch-s opened this issue May 14, 2017 · 15 comments
Closed

[Proposal] @dump blade directive #590

kleshch-s opened this issue May 14, 2017 · 15 comments

Comments

@kleshch-s
Copy link

I have created blade directive @Dump and tried to create pull request, but permission error has occured.
I propose to add @Dump blade directive. It can dump all available variables of the current view or user can specify a variable to dump.
It uses dump() funcion of VarDumper.
@dump to dump all available variables of current view
or
@dump($var) to dump specified variable
It outputs the following:
@dump
image link
@dump($var)
image link

In my opinion it's very usefull during development project.
Should I post code here or how I can add pull request?
Thanks

@sisve
Copy link

sisve commented May 15, 2017

Wouldn't this just be a simple <?php dump($var) ?>?

@kleshch-s
Copy link
Author

Almost. Additionally checking of var existing is required. And yes it is shortcode for ordinary dump.
But the main value of the directive is invoking without parametr to take all available variables. They are defined in composers, controllers, or some packages - doesn't metter - if some variable is available for your view - you will see it by @dump directive.

The complete code is

protected function compileDump($expression)
    {
        if (!$expression) {
            return '<?php dump(collect(get_defined_vars()["__data"])->filter(function ($value, $key) {return substr($key, 0, 2) !== "__";})->all()); ?>';
        }

        $varName = ltrim(rtrim($expression, ')'), '(');
        return "<?php dump(isset($varName) ? $varName : null); ?>";
    }

@ShahidH
Copy link

ShahidH commented May 15, 2017

It would be nice to include second param @dump($var, true) which mean it would not exit/die the application after dumping.

@sisve
Copy link

sisve commented May 15, 2017

@ShahidH It should never exit/die after dumping, because that is not what the dump() function does. You're thinking of dd() which is dump+die.

@kleshch-s
Copy link
Author

kleshch-s commented May 15, 2017

@ShahidH I agree with @sisve. But additionally I would like to add possibility to specify several variables. Like the following:
@dump($var1, $var2, ...)
or
@dump(['var1', 'var2', ...])

But first of all I need to know - how can I create a pull request to approve it. Should I assign this issue to someone?

Sorry, this is my first attempt to contribute to laravel framework and I haven't any idea what shoul I do

@m1guelpf
Copy link

@kleshch-s You can fork the framework repository, make your changes and then create a pull request.

@jerguslejko
Copy link

Sorry, I do not find it useful. Why add custom directive for code that will be deleted immediately?

{{ dd($_) }} or {{ dump($_) }} as be used instead? Maybe setup a shortcut in your editor?

@royduin
Copy link

royduin commented Nov 1, 2017

I think it's a nice addition to the framework, I'm currently using these directives: https://gist.github.com/cheelahim/e51d45f9c837254c8fe9 but renamed the d to dump to keep it in line with the helper.

No one created a pull request so far? Couldn't find one.

@sisve
Copy link

sisve commented Nov 2, 2017

Does this functionality make sense? Will you ever have a @dump or @dd in production code, or is it only used during development? Isn't <?php dump($var); ?> enough for temporary debugging stuff?

@royduin
Copy link

royduin commented Nov 2, 2017

It's just for rapid debugging / testing. Laravel does have a lot of useful helpers, so let's make them available in Blade with a simple directive. It will never make it to production or should be committed in a project but that's not a good argument if you ask me. In that case the dd() and dump() functions should be removed completely because those should never be used in production. And yes we could do {!! dump($var) !!} or <?php dump($var); ?> but why do we have Blade? To have a nice syntax for templating; so let's also use a nice syntax for debugging in Blade views.

@ajcastro
Copy link

ajcastro commented Nov 23, 2017

Also it is easier to type obviously. You can save your keyboard typings, lol.

@sleicester
Copy link

There is a specific issue at the moment, in that if you use {!! dump($var) !!} or {{ dump($var) }} and have run composer update recently this will pull in symfony/var-dumper version 3.4, which changes its behaviour to return the dumped variable. As the above blade statements echo before the dump this now fails for none string values being dumped (probably why you would use dump).

Details of this can be found in this issue here: Var Dumper - Backwards compatibility error in 3.3.x to 3.4.0

We do use this in production for an admin/backend area that allows us to confirm the output of web services as this display is completely fit for purpose.

I personally feel that a @dump directive would be a nice addition. It might be what I add myself as a Blade::directive() as mentioned above.

@Miguel-Serejo
Copy link

You can wrap it in a regular php block instead of {{}}.

@php
 dump($var);
@endphp

or simply <?php dump($var) ?>

@CupOfTea696
Copy link

CupOfTea696 commented Dec 13, 2017

Or use

{{ dump($var) ? '' : '' }}

or

@php(dump($var))

@gilbitron
Copy link

Added a PR for this laravel/framework#23364

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests