
Blade Syntax Extension
I want to share with my community my small library that adds control directives to the syntax of the Laravel-template engine Blade.
Immediately a small example of how it looks:
After meeting Angular.js, the familiar eye patterns began to seem illogical and uncomfortable. I wanted to manage tags and attributes, rather than treat HTML like a regular string. The benefit of Blade makes it easy to expand the functionality, which I took.
The library is trivially simple - all it does is convert attributes into the correct Blade syntax. So for example the code:
will be cast to:
Things are somewhat more complicated with tag attributes - here helper is used, which displays the attribute only if it contains a value.
bd-foreach , bd-inner-foreach
Repeats the entire tag (bd-foreach), or the entire contents of the tag (bd-inner-foreach) according to the specified values.
bd-if
Prints a tag or not depending on the condition.
bd-class
Adds one or more classes to an element, conditions can be used. If the tag already has a class attribute defined, then new classes will be added to it.
bd-attr-
Works the same as bd-class, but does not support multiple values and adding to an existing attribute.
bd-yield , bd-include
These directives add the appropriate Blade commands to the tag body.
bd-section
Frames the tag in the blade section.
The functionality of my library that extends the capabilities of Blade can also be expanded. You can register your attributes to be processed:
Sources on GitHub | Documentation and Examples
Immediately a small example of how it looks:
- {{{ $item }}}
After meeting Angular.js, the familiar eye patterns began to seem illogical and uncomfortable. I wanted to manage tags and attributes, rather than treat HTML like a regular string. The benefit of Blade makes it easy to expand the functionality, which I took.
Installation
- Add the “sleeping-owl / blade-extended” package to your composer.json.
- Add the Service provider to your “app / config / app.php” - 'SleepingOwl \ BladeExtended \ BladeExtendedServiceProvider'.
- Advanced syntax is now available to you.
How it works
The library is trivially simple - all it does is convert attributes into the correct Blade syntax. So for example the code:
{{{ $item }}}
will be cast to:
@if(test())@endif
@foreach($items as $item){{{ $item }}}@endforeach
Things are somewhat more complicated with tag attributes - here helper is used, which displays the attribute only if it contains a value.
Supported Directives
bd-foreach , bd-inner-foreach
Repeats the entire tag (bd-foreach), or the entire contents of the tag (bd-inner-foreach) according to the specified values.
bd-if
Prints a tag or not depending on the condition.
bd-class
Adds one or more classes to an element, conditions can be used. If the tag already has a class attribute defined, then new classes will be added to it.
bd-attr-
Works the same as bd-class, but does not support multiple values and adding to an existing attribute.
bd-yield , bd-include
These directives add the appropriate Blade commands to the tag body.
bd-section
Frames the tag in the blade section.
Want to extend the syntax?
The functionality of my library that extends the capabilities of Blade can also be expanded. You can register your attributes to be processed:
BladeExtended::extend('bd-test', function (BladeExtended $bladeExtended, &$finded)
{
$bladeExtended->wrapOuterContent($finded, '@if(myCustomTest())', '@endif');
});
Sources on GitHub | Documentation and Examples