Skip to content

Commit

Permalink
Consistently negate conditions in @includeUnless() Blade directive (#…
Browse files Browse the repository at this point in the history
…40077)

* Consistently negate conditions in @includeUnless() Blade directive

Before this, `@includeUnless($undefined ?? true, 'template')` would result
in a PHP Warning and an unexpected condition result, because
`$undefined ?? true` would be compiled as `! $undefined ?? true`, which due to
`!` having precedence over `??` is different than `! ($undefined ?? true)`
(which is how `@unless` would compile it).

This necessitated the creation of `renderUnless()`, because `@includeUnless()`
accepts multiple arguments, and parsing out the first argument to a Blade
directive is fraught. So `renderUnless()` takes the parameters as-is,
and negates the condition inside the method.

* DRY renderUnless() by having it use renderWhen()
markjaquith authored Dec 16, 2021
1 parent fa6921f commit ec85372
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Compilers/Concerns/CompilesIncludes.php
Original file line number Diff line number Diff line change
@@ -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'])); ?>";
}

/**
14 changes: 14 additions & 0 deletions Factory.php
Original file line number Diff line number Diff line change
@@ -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.
*

0 comments on commit ec85372

Please sign in to comment.