Skip to content

Commit 1f0bcbf

Browse files
hosnitaylorotwell
andauthored
[12.x] Fix using pushIf blade directive with complex conditions (#57264) (#57274)
* [12.x] Fix using pushIf blade directive with complex conditions (#57264) * Update CompilesConditionals.php * Update CompilesConditionals.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent f5c017e commit 1f0bcbf

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,16 @@ protected function compileSelected($condition)
379379
*/
380380
protected function compilePushIf($expression)
381381
{
382-
$parts = explode(',', $this->stripParentheses($expression), 2);
382+
$parts = explode(',', $this->stripParentheses($expression));
383+
384+
if (count($parts) > 2) {
385+
$last = array_pop($parts);
386+
387+
$parts = [
388+
implode(',', $parts),
389+
trim($last),
390+
];
391+
}
383392

384393
return "<?php if({$parts[0]}): \$__env->startPush({$parts[1]}); ?>";
385394
}

tests/View/Blade/BladePushTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ public function testPushIfIsCompiled()
7070
$this->assertEquals($expected, $this->compiler->compileString($string));
7171
}
7272

73+
public function testPushIfWithMoreThanOneCommaIsCompiled()
74+
{
75+
$string = '@pushIf(Str::startsWith(\'abc\', \'a\'), \'body-end\')
76+
test
77+
@endPushIf';
78+
79+
$expected = '<?php if(Str::startsWith(\'abc\', \'a\')): $__env->startPush(\'body-end\'); ?>
80+
test
81+
<?php $__env->stopPush(); endif; ?>';
82+
83+
$this->assertEquals($expected, $this->compiler->compileString($string));
84+
}
85+
86+
public function testPushIfWithCommaInStringIsCompiled()
87+
{
88+
$string = '@pushIf(Str::startsWith(\'abc,,,\', \'a,,,\'), \'body-end\')
89+
test
90+
@endPushIf';
91+
92+
$expected = '<?php if(Str::startsWith(\'abc,,,\', \'a,,,\')): $__env->startPush(\'body-end\'); ?>
93+
test
94+
<?php $__env->stopPush(); endif; ?>';
95+
96+
$this->assertEquals($expected, $this->compiler->compileString($string));
97+
}
98+
7399
public function testPushIfElseIsCompiled()
74100
{
75101
$string = '@pushIf(true, \'stack\')

0 commit comments

Comments
 (0)