Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blade templates broken when data comes from generator #23624

Closed
acarpio89 opened this issue Mar 20, 2018 · 1 comment
Closed

Blade templates broken when data comes from generator #23624

acarpio89 opened this issue Mar 20, 2018 · 1 comment

Comments

@acarpio89
Copy link
Contributor

  • Laravel Version: 5.5.37
  • PHP Version: 7.1.6
  • Database Driver & Version: MySQL 5.6.35

Description:

The changes introduced in #23388 break templates that loop over generators:

Exception(code: 0): Cannot traverse an already closed generator at ...

Steps To Reproduce:

In your view composer, or wherever the data for your view is prepared:

while ($someCondition) {
    ...
    yield $results;
}

In your view:

@foreach ($results as $chunk)   <<< This line throws the exception
    @foreach ($chunk as $record)
        ...
    @endforeach
@endforeach

The offending line looks like this in the compiled template:

<?php $__currentLoopData = $results; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $chunk): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
@acarpio89
Copy link
Contributor Author

This can be easily reproduced in a fresh Laravel project:

-- routes/web.php

Route::get('results', function () {
    return view('results');
});
-- app/Results.php

class Results
{
    public function generate()
    {
        for ($count = 1; $count < 10; $count ++) {
            yield ['a', 'b', 'c'];
        }
    }
}
-- resources/views/results.blade.php

@inject('results', 'App\Results')

@foreach($results->generate() as $chunks)
    <div>
        @foreach($chunks as $chunk)
            {{ $chunk }}
        @endforeach
    </div>
@endforeach

Using Laravel 5.5.36 you get the expected result:

a b c
a b c
a b c
a b c
a b c
a b c
a b c
a b c
a b c

Using any version after merging #23388 results in:

Cannot traverse an already closed generator (View: my-project/resources/views/results.blade.php)

It should be reverted until it provides a solution that doesn't break basic PHP functionality.

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

No branches or pull requests

1 participant