Skip to content

Commit

Permalink
Blade : add else in the custom if directive (#21611)
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Michotte <[email protected]>
  • Loading branch information
bmichotte authored and taylorotwell committed Oct 11, 2017
1 parent f2838f1 commit b9a3279
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ public function if($name, callable $callback)
: "<?php if (\Illuminate\Support\Facades\Blade::check('{$name}')): ?>";
});

$this->directive('else'.$name, function ($expression) use ($name) {
return $expression
? "<?php elseif (\Illuminate\Support\Facades\Blade::check('{$name}', {$expression})): ?>"
: "<?php elseif (\Illuminate\Support\Facades\Blade::check('{$name}')): ?>";
});

$this->directive('end'.$name, function () {
return '<?php endif; ?>';
});
Expand Down
17 changes: 17 additions & 0 deletions tests/View/Blade/BladeCustomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public function testCustomConditions()
$string = '@custom($user)
@endcustom';
$expected = '<?php if (\Illuminate\Support\Facades\Blade::check(\'custom\', $user)): ?>
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCustomIfElseConditions()
{
$this->compiler->if('custom', function ($anything) {
return true;
});

$string = '@custom($user)
@elsecustom($product)
@else
@endcustom';
$expected = '<?php if (\Illuminate\Support\Facades\Blade::check(\'custom\', $user)): ?>
<?php elseif (\Illuminate\Support\Facades\Blade::check(\'custom\', $product)): ?>
<?php else: ?>
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
Expand Down

0 comments on commit b9a3279

Please sign in to comment.