Skip to content

Commit

Permalink
Add custom "if" directive support
Browse files Browse the repository at this point in the history
  • Loading branch information
imliam committed Jul 10, 2019
1 parent 7219104 commit 06234bd
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ The changelog format is based on [Keep a Changelog](http://keepachangelog.com/en

...

## [1.1.0](https://github.com/imliam/laravel-blade-helper/releases/tag/v1.1.0) - 2019-07-10

## Added

- Added `BladeHelper::if` method

## [1.0.0](https://github.com/imliam/laravel-blade-helper/releases/tag/v1.0.0) - 2018-11-29

### Added

- Initial release
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ As this seems to be the most common use case, this package attempts to help make
- [Laravel Blade Helper](#laravel-blade-helper)
- [💾 Installation](#💾-installation)
- [📝 Usage](#📝-usage)
- [Example Helper Directive](#example-helper-directive)
- [Custom "if" Directive](#custom-if-directive)
- [✅ Testing](#✅-testing)
- [🔖 Changelog](#🔖-changelog)
- [⬆️ Upgrading](#⬆️-upgrading)
Expand All @@ -44,7 +46,7 @@ composer require imliam/laravel-blade-helper:^1.0.0

## 📝 Usage

The BladeHelper object is bound to Laravel's service container with the name `blade.helper` and can be used by resolving that. A Facade is also made available for convenience. To define a helper, the `->directive(…)` method is used:
The BladeHelper object is bound to Laravel's service container with the name `blade.helper` and can be used by resolving that. A Facade is also made available for convenience. To define a helper, the `directive(…)` method is used:

```php
app('blade.helper')->directive(…);
Expand Down Expand Up @@ -121,11 +123,13 @@ BladeHelper::directive('log', null, false);
// Nothing is echoed
```

### Example Helper Directive

One example of a custom Blade helper is to wrap around [FontAwesome 4](https://fontawesome.com/v4.7.0/) icons to make it more convenient to add alternate text for the sake of accessibility:

```php
// Define the helper directive
Blade::directive('fa', function(string $iconName, string $text = null, $classes = '') {
BladeHelper::directive('fa', function(string $iconName, string $text = null, $classes = '') {
if (is_array($classes)) {
$classes = join(' ', $classes);
}
Expand All @@ -139,6 +143,30 @@ Blade::directive('fa', function(string $iconName, string $text = null, $classes
@fa('email', 'Envelope')
```

### Custom "if" Directive

Laravel Blade offers [a handy way](https://laravel.com/docs/5.8/blade#custom-if-statements) to define custom "if" statement directives. The Blade Helper package offers an additional method to generate these directives, with `if`, `elseif` and `endif` variants all automatically generated.

An if statement can be defined in the same way as the directive method, but must be given a callable as its second argument:

```php
BladeHelper::if('largestFirst', function(int $a, int $b): bool {
return $a > $b;
});
```

Once defined, the helpers can be used directly in your Blade templates:

```html
@largestFirst(1, 2)
Lorem ipsum
@elseLargestFirst(5, 3)
dolor sit amet
@else
consectetur adipiscing elit
@endLargestFirst
```

## ✅ Testing

``` bash
Expand Down
25 changes: 25 additions & 0 deletions src/BladeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,29 @@ public function getDirective(string $name, ...$arguments)
{
return $this->customDirectives[$name](...$arguments);
}

/**
* Register an "if" statement directive.
*
* @param string $directiveName
* @param callable $function
*
* @return void
*/
public function if(string $directiveName, callable $function)
{
$this->customDirectives[$directiveName] = $function;

$this->compiler->directive($directiveName, function ($expression) use ($directiveName) {
return "<?php if (app('blade.helper')->getDirective('{$directiveName}', {$expression})): ?>";
});

$this->compiler->directive('else' . ucfirst($directiveName), function ($expression) use ($directiveName) {
return "<?php elseif (app('blade.helper')->getDirective('{$directiveName}', {$expression})): ?>";
});

$this->compiler->directive('end' . ucfirst($directiveName), function () {
return "<?php endif; ?>";
});
}
}
37 changes: 34 additions & 3 deletions tests/Unit/BladeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class BladeHelperTest extends TestCase

protected $helper;

public function setUp()
public function setUp(): void
{
$this->compiler = new BladeCompiler(m::mock('Illuminate\Filesystem\Filesystem'), __DIR__);
$this->helper = new BladeHelper($this->compiler);
parent::setUp();
}

public function tearDown()
public function tearDown(): void
{
m::close();
parent::tearDown();
Expand Down Expand Up @@ -70,6 +70,37 @@ public function custom_helper_callbacks_compile_correctly()
$string = '@example("Never", "gonna")';
$expected = '<?php echo app(\'blade.helper\')->getDirective(\'example\', "Never", "gonna"); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
echo $expected;
}

/** @test */
public function custom_if_helpers_can_be_registered()
{
$this->assertCount(0, $this->compiler->getCustomDirectives());
$this->helper->if('largestFirst', function ($a, $b) {
return $a > $b;
});
$this->assertCount(3, $this->compiler->getCustomDirectives());

$string = <<<EOL
@largestFirst(1, 2)
Lorem ipsum
@elseLargestFirst(5, 3)
dolor sit amet
@else
consectetur adipiscing elit
@endLargestFirst
EOL;

$expected = <<<EOL
<?php if (app('blade.helper')->getDirective('largestFirst', 1, 2)): ?>
Lorem ipsum
<?php elseif (app('blade.helper')->getDirective('largestFirst', 5, 3)): ?>
dolor sit amet
<?php else: ?>
consectetur adipiscing elit
<?php endif; ?>
EOL;

$this->assertEquals($expected, $this->compiler->compileString($string));
}
}

0 comments on commit 06234bd

Please sign in to comment.