PHPackages                             brunocfalcao/laravel-blade-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. brunocfalcao/laravel-blade-helper

ActiveLibrary[Templating &amp; Views](/categories/templating)

brunocfalcao/laravel-blade-helper
=================================

An easier way to define custom Blade directives.

013PHP

Since Mar 17Pushed 2y agoCompare

[ Source](https://github.com/brunocfalcao/laravel-blade-helper)[ Packagist](https://packagist.org/packages/brunocfalcao/laravel-blade-helper)[ RSS](/packages/brunocfalcao-laravel-blade-helper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Blade Helper
====================

[](#laravel-blade-helper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b69c0f243964ba2e94d5112a92d92f9a25faba0669c7980c5499d7b462f3a61d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d6c69616d2f6c61726176656c2d626c6164652d68656c7065722e737667)](https://packagist.org/packages/imliam/laravel-blade-helper)[![Build Status](https://camo.githubusercontent.com/6bf2e0ed0f465d787e9eb82c26836c551a7e2da5e563d44253efafb1ccddbedf/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f696d6c69616d2f6c61726176656c2d626c6164652d68656c7065722e737667)](https://travis-ci.org/imliam/laravel-blade-helper)[![CI Status](https://github.com/imliam/laravel-blade-helper/workflows/Run%20Tests/badge.svg)](https://github.com/imliam/laravel-blade-helper/actions)[![Total Downloads](https://camo.githubusercontent.com/28f73a48a8abb20b30513c3ac9451ebd34dc2ee0097a15dcaa1e2de7a30e57d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696d6c69616d2f6c61726176656c2d626c6164652d68656c7065722e737667)](https://packagist.org/packages/imliam/laravel-blade-helper)[![License](https://camo.githubusercontent.com/125725acd4c00abea8791bbe22c0601ae4be364fe7daca90a1f97e6b0027815c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696d6c69616d2f6c61726176656c2d626c6164652d68656c7065722e737667)](LICENSE.md)

An easier way to define custom Blade directives.

When creating new custom Blade directives using the `Blade::directive(…)` method, the only parameter made available to manipulate is the expression passed through from the .blade.php file as a raw string. It seems to be rare that developers actually parse the contents of the expression itself within the directive, opting instead to pass the entire expression as arguments to a helper function or a method on another class. For example:

```
Illuminate\Support\Facades\Blade::directive('uppercase', function($expression) {
    return "";
});
```

As this seems to be the most common use case, this package attempts to help make these helper functions that little bit easier to define without the boilerplate of returning the string or having to consider what an expression may be when creating a directive.

- [Laravel Blade Helper](#laravel-blade-helper)
    - [💾 Installation](#%F0%9F%92%BE-installation)
    - [📝 Usage](#%F0%9F%93%9D-usage)
        - [Example Helper Directive](#example-helper-directive)
        - [Custom "if" Directive](#custom-if-directive)
    - [✅ Testing](#%E2%9C%85-testing)
    - [🔖 Changelog](#%F0%9F%94%96-changelog)
    - [⬆️ Upgrading](#%E2%AC%86%EF%B8%8F-upgrading)
    - [🎉 Contributing](#%F0%9F%8E%89-contributing)
        - [🔒 Security](#%F0%9F%94%92-security)
    - [👷 Credits](#%F0%9F%91%B7-credits)
    - [♻️ License](#%E2%99%BB%EF%B8%8F-license)

💾 Installation
--------------

[](#-installation)

You can install the package with [Composer](https://getcomposer.org/) using the following command:

```
composer require imliam/laravel-blade-helper:^1.0
```

📝 Usage
-------

[](#-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:

```
app('blade.helper')->directive(…);

\ImLiam\BladeHelper\Facades\BladeHelper::directive(…);
```

This method accepts two arguments; the first is the name of the directive, and the second is the function that the directive should call:

```
// Define the helper directive
BladeHelper::directive('uppercase', 'strtoupper');

// Use it in a view
@uppercase('Hello world.')

// Get the compiled result
