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:

    • {{{ $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.


    1. Add the “sleeping-owl / blade-extended” package to your composer.json.
    2. Add the Service provider to your “app / config / app.php” - 'SleepingOwl \ BladeExtended \ BladeExtendedServiceProvider'.
    3. 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:

    @foreach($items as $item)
    {{{ $item }}}

    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.

    Prints a tag or not depending on the condition.

    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.

    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.

    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

    Also popular now: