laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Alternative to @include() whose variable scope isn't affected by parent template #2304

Open senyahnoj opened 4 years ago

senyahnoj commented 4 years ago

This reddit post sums up the issue quite well https://www.reddit.com/r/laravel/comments/8y4qpg/how_can_i_more_elegantly_include_a_subview/

If you use @include() with lots of nested blade templates then there is a risk of changing a variable in a parent template or the controller has an unexpected consequence. For this reason it is desirable to be explicit in the code exactly what variables are passed into an include.

In our code we use the following custom blade template function:

// Custom blade @incl for an include which doesn't inherit the parent scope (unlike @include which does) Blade::directive('incl', function ($expression) { return "<?php echo \$__env->make({$expression})->render(); ?>"; });

It would be nice if something similar was considered for including in the main laravel framework.

ho0406 commented 4 years ago

Usually, I will use @component to pass the explicit variables