Download the PHP package imliam/laravel-blade-helper without Composer
On this page you can find all versions of the php package imliam/laravel-blade-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download imliam/laravel-blade-helper
More information about imliam/laravel-blade-helper
Files in imliam/laravel-blade-helper
Package laravel-blade-helper
Short Description An easier way to define custom Blade directives.
License MIT
Homepage https://github.com/imliam/laravel-blade-helper
Informations about the package laravel-blade-helper
Laravel Blade Helper
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:
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
- 💾 Installation
- 📝 Usage
- Example Helper Directive
- Custom "if" Directive
- ✅ Testing
- 🔖 Changelog
- ⬆️ Upgrading
- 🎉 Contributing
- 🔒 Security
- 👷 Credits
- ♻️ License
💾 Installation
You can install the package with Composer using the following command:
📝 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:
This method accepts two arguments; the first is the name of the directive, and the second is the function that the directive should call:
If no second argument is supplied, the directive will attempt to call a function of the same name:
The second argument can also take a callback. The advantage of a callback here over the typical Blade::directive(…)
method Laravel offers is that the callback given can have specific parameters defined instead of just getting raw expression as a string. This brings several advantages to the process of creating a Blade helper directive:
- Type hint the arguments for the callback
- Manipulate and use the individual arguments when the directive is called, instead of the raw expression as a string
- Define a directive without having to only use it as a proxy to a helper function or class in another part of the application
By default, all of the helper directives will echo out their contents to the view when used. This can be disabled by passing false
as the third argument:
Example Helper Directive
One example of a custom Blade helper is to wrap around FontAwesome 4 icons to make it more convenient to add alternate text for the sake of accessibility:
Custom "if" Directive
Laravel Blade offers a handy way 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:
Once defined, the helpers can be used directly in your Blade templates:
✅ Testing
🔖 Changelog
Please see the changelog file for more information on what has changed recently.
⬆️ Upgrading
Please see the upgrading file for details on upgrading from previous versions.
🎉 Contributing
Please see the code of conduct for details on contributing to the project.
🔒 Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
👷 Credits
- Liam Hammett
- @bhuvidya for initially extracting the original PR to a package
- All Contributors
♻️ License
The MIT License (MIT). Please see the license file for more information.