Skip to content

Commit 3687363

Browse files
Lukas Kämmerlingtaylorotwell
Lukas Kämmerling
authored andcommitted
[5.4] Implement standalone if empty in a NB-Change-Way (#18738)
* 5.4 Implement if empty in as NB-Change * Fix little StyleCI Issue * Fix little StyleCI Issue #2 * Fix little StyleCI Issue #3 * Update CompilesLoops.php
1 parent 73c673e commit 3687363

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ protected function compileForelse($expression)
3535
}
3636

3737
/**
38-
* Compile the for-else-empty statements into valid PHP.
38+
* Compile the for-else-empty and empty statements into valid PHP.
3939
*
40+
* @param string $expression
4041
* @return string
4142
*/
42-
protected function compileEmpty()
43+
protected function compileEmpty($expression)
4344
{
45+
if ($expression) {
46+
return "<?php if(empty{$expression}): ?>";
47+
}
48+
4449
$empty = '$__empty_'.$this->forElseCounter--;
4550

4651
return "<?php endforeach; \$__env->popLoop(); \$loop = \$__env->getLastLoop(); if ({$empty}): ?>";
@@ -56,6 +61,16 @@ protected function compileEndforelse()
5661
return '<?php endif; ?>';
5762
}
5863

64+
/**
65+
* Compile the end-empty statements into valid PHP.
66+
*
67+
* @return string
68+
*/
69+
protected function compileEndEmpty()
70+
{
71+
return '<?php endif; ?>';
72+
}
73+
5974
/**
6075
* Compile the for statements into valid PHP.
6176
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Blade;
4+
5+
use Mockery as m;
6+
use PHPUnit\Framework\TestCase;
7+
use Illuminate\View\Compilers\BladeCompiler;
8+
9+
class BladeIfEmptyStatementsTest extends TestCase
10+
{
11+
public function tearDown()
12+
{
13+
m::close();
14+
}
15+
16+
public function testIfStatementsAreCompiled()
17+
{
18+
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
19+
$string = '@empty ($test)
20+
breeze
21+
@endempty';
22+
$expected = '<?php if(empty($test)): ?>
23+
breeze
24+
<?php endif; ?>';
25+
$this->assertEquals($expected, $compiler->compileString($string));
26+
}
27+
28+
protected function getFiles()
29+
{
30+
return m::mock('Illuminate\Filesystem\Filesystem');
31+
}
32+
}

0 commit comments

Comments
 (0)