Skip to content

[5.2] Blade push with layouts weird ordering #12769

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ protected function compileStack($expression)
*/
protected function compilePush($expression)
{
return "<?php \$__env->startSection{$expression}; ?>";
return "<?php \$__env->startSectionPush{$expression}; ?>";
}

/**
Expand Down
76 changes: 65 additions & 11 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,34 @@ public function callCreator(View $view)
*/
public function startSection($section, $content = '')
{
if (! isset($this->sections[$section])) {
$this->sections[$section] = [];
$this->sections[$section][0] = null;
}
if ($content === '') {
if (ob_start()) {
$this->sectionStack[] = $section;
}
} else {
$this->extendSection($section, $content);
}
}

/**
* Start injecting content into a push section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startSectionPush($section, $content = '')
{
if (! isset($this->sections[$section])) {
$this->sections[$section] = [];
}
if (! isset($this->sections[$section][$this->renderCount])) {
$this->sections[$section][$this->renderCount] = null;
}
if ($content === '') {
if (ob_start()) {
$this->sectionStack[] = $section;
Expand Down Expand Up @@ -567,7 +595,7 @@ public function stopSection($overwrite = false)
$last = array_pop($this->sectionStack);

if ($overwrite) {
$this->sections[$last] = ob_get_clean();
$this->setSectionContent($last, ob_get_clean());
} else {
$this->extendSection($last, ob_get_clean());
}
Expand All @@ -589,11 +617,7 @@ public function appendSection()

$last = array_pop($this->sectionStack);

if (isset($this->sections[$last])) {
$this->sections[$last] .= ob_get_clean();
} else {
$this->sections[$last] = ob_get_clean();
}
$this->setSectionContent($last, ob_get_clean(), true);

return $last;
}
Expand All @@ -607,11 +631,10 @@ public function appendSection()
*/
protected function extendSection($section, $content)
{
if (isset($this->sections[$section])) {
$content = str_replace('@parent', $content, $this->sections[$section]);
if (! is_null($this->getSectionContent($section))) {
$content = str_replace('@parent', $content, $this->getSectionContent($section));
}

$this->sections[$section] = $content;
$this->setSectionContent($section, $content);
}

/**
Expand All @@ -626,7 +649,7 @@ public function yieldContent($section, $default = '')
$sectionContent = $default;

if (isset($this->sections[$section])) {
$sectionContent = $this->sections[$section];
$sectionContent = implode(array_reverse($this->sections[$section]));
}

$sectionContent = str_replace('@@parent', '--parent--holder--', $sectionContent);
Expand Down Expand Up @@ -874,6 +897,37 @@ public function getSections()
return $this->sections;
}

/**
* Get the section from the last element.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing newline

*
* @param string $section
* @return string|null
*/
public function getSectionContent($section)
{
return end($this->sections[$section]);
}

/**
* Set the section to the last element.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, fix two

*
* @param string $section
* @param string $content
* @param bool $append
* @return void
*/
public function setSectionContent($section, $content, $append = false)
{
end($this->sections[$section]);
$key = key($this->sections[$section]);

if (is_null($this->sections[$section][$key]) || $append === false) {
$this->sections[$section][$key] = $content;
} else {
$this->sections[$section][$key] .= $content;
}
}

/**
* Get all of the registered named views in environment.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function testPushIsCompiled()
$string = '@push(\'foo\')
test
@endpush';
$expected = '<?php $__env->startSection(\'foo\'); ?>
$expected = '<?php $__env->startSectionPush(\'foo\'); ?>
test
<?php $__env->appendSection(); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
Expand Down