Skip to content

Commit b194d3f

Browse files
authored
[12.x] create new @hasStack Blade directive (#57788)
* create new `@hasStack` Blade directive this new `@hasStack` directive can be used to wrap `@stack` directives for conditional output depending on if the stack is empty or not. this can be useful if you want some wrapping code around the stack only if it is not empty. mimicked the `@hasSection` directive for naming. * minor formatting
1 parent 5d05f85 commit b194d3f

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ protected function compileHasSection($expression)
137137
return "<?php if (! empty(trim(\$__env->yieldContent{$expression}))): ?>";
138138
}
139139

140+
/**
141+
* Compile the has-stack statements into valid PHP.
142+
*
143+
* @param string $expression
144+
* @return string
145+
*/
146+
protected function compileHasStack($expression)
147+
{
148+
return "<?php if (! \$__env->isStackEmpty{$expression}): ?>";
149+
}
150+
140151
/**
141152
* Compile the section-missing statements into valid PHP.
142153
*

src/Illuminate/View/Concerns/ManagesStacks.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function extendPrepend($section, $content)
148148
*/
149149
public function yieldPushContent($section, $default = '')
150150
{
151-
if (! isset($this->pushes[$section]) && ! isset($this->prepends[$section])) {
151+
if ($this->isStackEmpty($section)) {
152152
return $default;
153153
}
154154

@@ -165,6 +165,14 @@ public function yieldPushContent($section, $default = '')
165165
return $output;
166166
}
167167

168+
/**
169+
* Determine if the stack has any content in it.
170+
*/
171+
public function isStackEmpty(string $section): bool
172+
{
173+
return ! isset($this->pushes[$section]) && ! isset($this->prepends[$section]);
174+
}
175+
168176
/**
169177
* Flush all of the stacks.
170178
*
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\View\Blade;
4+
5+
class BladeHasStackTest extends AbstractBladeTestCase
6+
{
7+
public function testHasStackStatementsAreCompiled()
8+
{
9+
$string = '@hasStack("stack")
10+
breeze
11+
@endif';
12+
$expected = '<?php if (! $__env->isStackEmpty("stack")): ?>
13+
breeze
14+
<?php endif; ?>';
15+
$this->assertEquals($expected, $this->compiler->compileString($string));
16+
}
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\View\Concerns;
4+
5+
use Illuminate\View\Concerns\ManagesStacks;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ManagesStacksTest extends TestCase
9+
{
10+
public function testStackIsEmpty()
11+
{
12+
$this->assertTrue((new FakeViewFactory)->isStackEmpty('my-stack'));
13+
}
14+
15+
public function testStackIsNotEmptyWithPushedContent()
16+
{
17+
$object = new FakeViewFactory;
18+
$object->startPush('my-stack', 'some pushed content');
19+
20+
$this->assertFalse($object->isStackEmpty('my-stack'));
21+
}
22+
23+
public function testStackIsNotEmptyWithPrependedContent()
24+
{
25+
$object = new FakeViewFactory;
26+
$object->startPrepend('my-stack', 'some prepended content');
27+
28+
$this->assertFalse($object->isStackEmpty('my-stack'));
29+
}
30+
}
31+
32+
class FakeViewFactory
33+
{
34+
use ManagesStacks;
35+
36+
protected int $renderCount = 0;
37+
}

0 commit comments

Comments
 (0)