Skip to content

Commit

Permalink
Add includeWhen directive
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk authored and taylorotwell committed Feb 22, 2017
1 parent 2f4135d commit 5fe39aa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,23 @@ protected function compileIncludeIf($expression)

return "<?php if (\$__env->exists({$expression})) echo \$__env->make({$expression}, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
}

/**
* Compile the include-when statements into valid PHP.
*
* @param string $expression
* @return string
*/
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(); ?>";
}
}
27 changes: 27 additions & 0 deletions tests/View/Blade/BladeIncludeWhenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Illuminate\Tests\Blade;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\View\Compilers\BladeCompiler;

class BladeIncludeWhenTest extends TestCase
{
public function tearDown()
{
m::close();
}

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))'));
}

protected function getFiles()
{
return m::mock('Illuminate\Filesystem\Filesystem');
}
}

0 comments on commit 5fe39aa

Please sign in to comment.