Skip to content

Commit 69a2a9f

Browse files
themsaidtaylorotwell
authored andcommitted
let PHP parse renderWhen directive (#18285)
1 parent 5d9b363 commit 69a2a9f

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ protected function compileIncludeWhen($expression)
5151
{
5252
$expression = $this->stripParentheses($expression);
5353

54-
preg_match('/ *(.*), *(.*)$/is', $expression, $matches);
55-
56-
$when = trim($matches[1]);
57-
58-
$arguments = trim($matches[2]);
59-
60-
return "<?php if ({$when}) echo \$__env->make({$arguments}, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
54+
return "<?php echo \$__env->renderWhen($expression, array_except(get_defined_vars(), array('__data', '__path'))); ?>";
6155
}
6256
}

src/Illuminate/View/Factory.php

+18
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,24 @@ public function make($view, $data = [], $mergeData = [])
138138
});
139139
}
140140

141+
/**
142+
* Get the rendered content of the view based on a given condition.
143+
*
144+
* @param bool $condition
145+
* @param string $view
146+
* @param array $data
147+
* @param array $mergeData
148+
* @return string
149+
*/
150+
public function renderWhen($condition, $view, $data = [], $mergeData = [])
151+
{
152+
if (! $condition) {
153+
return '';
154+
}
155+
156+
return $this->make($view, $this->parseData($data), $mergeData)->render();
157+
}
158+
141159
/**
142160
* Get the rendered contents of a partial from a loop.
143161
*

tests/View/Blade/BladeIncludeWhenTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ public function tearDown()
1616
public function testIncludeWhensAreCompiled()
1717
{
1818
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
19-
$this->assertEquals('<?php if (true) echo $__env->make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@includeWhen(true, \'foo\')'));
20-
$this->assertEquals('<?php if (true) echo $__env->make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@includeWhen(true, name(foo))'));
21-
$this->assertEquals('<?php if (foo((\'foo\'))) echo $__env->make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@includeWhen(foo((\'foo\')), name(foo))'));
22-
$this->assertEquals('<?php if (\'true, false\') echo $__env->make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@includeWhen(\'true, false\', name(foo))'));
19+
$this->assertEquals('<?php echo $__env->renderWhen(true, \'foo\', ["foo" => "bar"], array_except(get_defined_vars(), array(\'__data\', \'__path\'))); ?>', $compiler->compileString('@includeWhen(true, \'foo\', ["foo" => "bar"])'));
20+
$this->assertEquals('<?php echo $__env->renderWhen(true, \'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\'))); ?>', $compiler->compileString('@includeWhen(true, \'foo\')'));
2321
}
2422

2523
protected function getFiles()

0 commit comments

Comments
 (0)