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 @forelse #5028

Merged
merged 1 commit into from
Jul 11, 2014
Merged
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
24 changes: 23 additions & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ protected function compileFor($expression)
*/
protected function compileForeach($expression)
{
return "<?php foreach{$expression}: ?>";
return "<?php \$__empty = true; foreach{$expression}: \$__empty = false; ?>";
}

/**
Expand All @@ -453,6 +453,17 @@ protected function compileElseif($expression)
return "<?php elseif{$expression}: ?>";
}

/**
* Compile the forelse statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileForelse($expression)
{
return "<?php endforeach; if (\$__empty): ?>";
}

/**
* Compile the while statements into valid PHP.
*
Expand Down Expand Up @@ -508,6 +519,17 @@ protected function compileEndif($expression)
return "<?php endif; ?>";
}

/**
* Compile the end-for-else statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndforelse($expression)
{
return "<?php endif; ?>";
}

/**
* Compile the extends statements into valid PHP.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@ public function testUnlessStatementsAreCompiled()
}


public function testForelseStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@foreach ($this->getUsers() as $user)
breeze
@forelse
empty
@endforelse';
$expected = '<?php $__empty = true; foreach($this->getUsers() as $user): $__empty = false; ?>
breeze
<?php endforeach; if ($__empty): ?>
empty
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}


public function testStatementThatContainsNonConsecutiveParanthesisAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
Expand Down