Skip to content

Commit

Permalink
[5.6] fix custom blade conditional ignoring 0 as argument (#24394)
Browse files Browse the repository at this point in the history
* fixed bug where 0 as an argument to a custom blade conditional was ignored

* added missing else conditional test
  • Loading branch information
tjallingt authored and taylorotwell committed May 31, 2018
1 parent 36f8102 commit 9983fd8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,13 @@ public function if($name, callable $callback)
$this->conditions[$name] = $callback;

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

$this->directive('else'.$name, function ($expression) use ($name) {
return $expression
return $expression !== ''
? "<?php elseif (\Illuminate\Support\Facades\Blade::check('{$name}', {$expression})): ?>"
: "<?php elseif (\Illuminate\Support\Facades\Blade::check('{$name}')): ?>";
});
Expand Down
15 changes: 15 additions & 0 deletions tests/View/Blade/BladeCustomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public function testCustomIfElseConditions()
$this->assertEquals($expected, $this->compiler->compileString($string));
}

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

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

public function testCustomComponents()
{
$this->compiler->component('app.components.alert', 'alert');
Expand Down

0 comments on commit 9983fd8

Please sign in to comment.