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

[8.x] Consistently negate conditions in @includeUnless() Blade directive #40077

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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function compileIncludeUnless($expression)
{
$expression = $this->stripParentheses($expression);

return "<?php echo \$__env->renderWhen(! $expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>";
return "<?php echo \$__env->renderUnless($expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>";
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ public function renderWhen($condition, $view, $data = [], $mergeData = [])
return $this->make($view, $this->parseData($data), $mergeData)->render();
}

/**
* Get the rendered content of the view based on the negation of a given condition.
*
* @param bool $condition
* @param string $view
* @param \Illuminate\Contracts\Support\Arrayable|array $data
* @param array $mergeData
* @return string
*/
public function renderUnless($condition, $view, $data = [], $mergeData = [])
{
return $this->renderWhen(! $condition, $view, $data, $mergeData);
}

/**
* Get the rendered contents of a partial from a loop.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/View/Blade/BladeIncludesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public function testIncludeWhensAreCompiled()
$this->assertSame('<?php echo $__env->renderWhen(true, \'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>', $this->compiler->compileString('@includeWhen(true, \'foo\')'));
}

public function testIncludeUnlessesAreCompiled()
{
$this->assertSame('<?php echo $__env->renderUnless(true, \'foo\', ["foo" => "bar"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>', $this->compiler->compileString('@includeUnless(true, \'foo\', ["foo" => "bar"])'));
$this->assertSame('<?php echo $__env->renderUnless($undefined ?? true, \'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>', $this->compiler->compileString('@includeUnless($undefined ?? true, \'foo\')'));
}

public function testIncludeFirstsAreCompiled()
{
$this->assertSame('<?php echo $__env->first(["one", "two"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeFirst(["one", "two"])'));
Expand Down