Skip to content

Commit

Permalink
add if method
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 23, 2017
1 parent 35bbae4 commit 71dfe0f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\View\Compilers;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -345,6 +346,40 @@ public function getExtensions()
return $this->extensions;
}

/**
* Register an "if" statement directive.
*
* @param string $name

This comment has been minimized.

Copy link
@fonini

fonini Jun 23, 2017

Typo

* @param \Closrue $callback
* @return void
*/
public function if($name, Closure $callback)
{
$this->conditions[$name] = $callback;

$this->directive($name, function ($expression) use ($name) {
return $expression
? "<?php if (\Illuminate\Support\Facades\Blade::check('{$name}', {$expression})): ?>"
: "<?php if (\Illuminate\Support\Facades\Blade::check('{$name}')): ?>";
});

$this->directive('end'.$name, function () {
return "<?php endif; ?>";
});
}

/**
* Check the result of a condition.
*
* @param string $name
* @param array $parameters
* @return bool
*/
public function check($name, ...$parameters)
{
return call_user_func($this->conditions[$name], ...$parameters);
}

/**
* Register a handler for custom directives.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/View/Blade/BladeCustomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,17 @@ public function testCustomExtensionOverwritesCore()
$expected = '<?php custom(); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCustomConditions()
{
$this->compiler->if('custom', function ($user) {
return true;
});

$string = '@custom($user)
@endcustom';
$expected = '<?php if (\Illuminate\Support\Facades\Blade::check(\'custom\', $user)): ?>
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}

0 comments on commit 71dfe0f

Please sign in to comment.