Framework: Blade overwriting $loop variable

Created on 12 Apr 2017  路  7Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.*
  • PHP Version: 7.1
  • Database Driver & Version: MySQL 5.6

Description:

Blade's $loop variable is interfering with my own code. I have my own $loop variable (which is an unrelated object), and whenever I call @foreach, Blade is overwriting my $loop. I know that I should 'suck it up', and name my variable something else. However, that would limit my code readability and force me to come up with a hacky workaround. I feel that Laravel should be more careful in terms of name collisions like this. Would it be possible to rename it to $_loop to avoid this happening?

Steps To Reproduce:

Name any variable $loop prior to using @foreach or similar in a Blade template.

Most helpful comment

It's a framework. It makes sense to have reserved variable names that are common.
The idea is to cover the 90% of the use cases.

If you want the variable to be prefixed by "laravel" or anything else, I'd advise you to close this issue and open a new one here: https://github.com/laravel/internals/issues

All 7 comments

That would break everyones code, if they are already using $loop->foo. Just choose a different name.

I'm thinking this would be for a future release. Is there a way that I could potentially override it for myself?

I don't see why this is a bug. You are using a variable that blade requires it will for sure mess up with things.

This is not a bug, consider $loop as a reserved variable name just like $message is reserved for email views.

I'm not saying it's a bug, I'm just trying to come up with a namespacing for Laravel's reserved variables. This is way more widespread than just $loop. $message and $error also have this functionality. I understand why a change could complicate things, but a reserved variable name -- especially a name that is as common as these -- seems ridiculous to me.

It's a framework. It makes sense to have reserved variable names that are common.
The idea is to cover the 90% of the use cases.

If you want the variable to be prefixed by "laravel" or anything else, I'd advise you to close this issue and open a new one here: https://github.com/laravel/internals/issues

Thanks, @Belphemur. Will do!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Anahkiasen picture Anahkiasen  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

iivanov2 picture iivanov2  路  3Comments

fideloper picture fideloper  路  3Comments