Skip to content
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
8 changes: 1 addition & 7 deletions src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ protected function compileIncludeWhen($expression)
{
$expression = $this->stripParentheses($expression);

preg_match('/ *(.*), *(.*)$/is', $expression, $matches);

$when = trim($matches[1]);

$arguments = trim($matches[2]);

return "<?php if ({$when}) echo \$__env->make({$arguments}, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
return "<?php echo \$__env->renderWhen($expression, array_except(get_defined_vars(), array('__data', '__path'))); ?>";
}
}
18 changes: 18 additions & 0 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ public function make($view, $data = [], $mergeData = [])
});
}

/**
* Get the rendered content of the view based on a given condition.
*
* @param bool $condition
* @param string $view
* @param array $data
* @param array $mergeData
* @return string
*/
public function renderWhen($condition, $view, $data = [], $mergeData = [])
{
if (! $condition) {
return '';
}

return $this->make($view, $this->parseData($data), $mergeData)->render();
}

/**
* Get the rendered contents of a partial from a loop.
*
Expand Down
6 changes: 2 additions & 4 deletions tests/View/Blade/BladeIncludeWhenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ public function tearDown()
public function testIncludeWhensAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php if (true) echo $__env->make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@includeWhen(true, \'foo\')'));
$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))'));
$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))'));
$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))'));
$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"])'));
$this->assertEquals('<?php echo $__env->renderWhen(true, \'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\'))); ?>', $compiler->compileString('@includeWhen(true, \'foo\')'));
}

protected function getFiles()
Expand Down