diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php b/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php index 591a1330b9ff..3a205d6eccb7 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php @@ -137,6 +137,17 @@ protected function compileHasSection($expression) return "yieldContent{$expression}))): ?>"; } + /** + * Compile the has-stack statements into valid PHP. + * + * @param string $expression + * @return string + */ + protected function compileHasStack($expression) + { + return "isStackEmpty{$expression}): ?>"; + } + /** * Compile the section-missing statements into valid PHP. * diff --git a/src/Illuminate/View/Concerns/ManagesStacks.php b/src/Illuminate/View/Concerns/ManagesStacks.php index 4e063af1e943..aecff30f7178 100644 --- a/src/Illuminate/View/Concerns/ManagesStacks.php +++ b/src/Illuminate/View/Concerns/ManagesStacks.php @@ -148,7 +148,7 @@ protected function extendPrepend($section, $content) */ public function yieldPushContent($section, $default = '') { - if (! isset($this->pushes[$section]) && ! isset($this->prepends[$section])) { + if ($this->isStackEmpty($section)) { return $default; } @@ -165,6 +165,14 @@ public function yieldPushContent($section, $default = '') return $output; } + /** + * Determine if the stack has any content in it. + */ + public function isStackEmpty(string $section): bool + { + return ! isset($this->pushes[$section]) && ! isset($this->prepends[$section]); + } + /** * Flush all of the stacks. * diff --git a/tests/View/Blade/BladeHasStackTest.php b/tests/View/Blade/BladeHasStackTest.php new file mode 100644 index 000000000000..b540f50d2384 --- /dev/null +++ b/tests/View/Blade/BladeHasStackTest.php @@ -0,0 +1,17 @@ +isStackEmpty("stack")): ?> +breeze +'; + $this->assertEquals($expected, $this->compiler->compileString($string)); + } +} diff --git a/tests/View/Concerns/ManagesStacksTest.php b/tests/View/Concerns/ManagesStacksTest.php new file mode 100644 index 000000000000..3861aba720ee --- /dev/null +++ b/tests/View/Concerns/ManagesStacksTest.php @@ -0,0 +1,37 @@ +assertTrue((new FakeViewFactory)->isStackEmpty('my-stack')); + } + + public function testStackIsNotEmptyWithPushedContent() + { + $object = new FakeViewFactory; + $object->startPush('my-stack', 'some pushed content'); + + $this->assertFalse($object->isStackEmpty('my-stack')); + } + + public function testStackIsNotEmptyWithPrependedContent() + { + $object = new FakeViewFactory; + $object->startPrepend('my-stack', 'some prepended content'); + + $this->assertFalse($object->isStackEmpty('my-stack')); + } +} + +class FakeViewFactory +{ + use ManagesStacks; + + protected int $renderCount = 0; +}