jeroen-g / blade-macro
Create small, file-specific, macros in Blade like Twig has.
1.1.0
2024-03-18 10:39 UTC
Requires
- php: 8.0.*||8.1.*||8.2.*||8.3.*
- illuminate/support: ^9.0||^10.0||^11.0
Requires (Dev)
- infection/infection: ^0.26
- mockery/mockery: ^1.5
- phpunit/phpunit: ~9.0
- symplify/easy-coding-standard: ^9.0
README
Have you as a Laravel developer ever looked at Twig macros with envy?
Maybe you want to reuse a specific element twice on the same page, but you don't want to have it (supported) as a Blade component. With a Blade macro, you can. Here is an example:
@macro(listItem) <li class="flex justify-center all-sorts-of-tailwind-classes"> {{ $name }} </li> @endmacro @if($conditionIsTrue) <ul> @foreach($listOne as $name) @showMacro(listItem, ['name' => $name]) @endforeach </ul> @else <ul> @foreach($listTwo as $name) @showMacro(listItem, ['name' => $name]) @endforeach </ul> @endif
Installation
Via Composer
$ composer require jeroen-g/blade-macro
Usage
Define a macro with the Blade directives:
@macro(myMacroName) <fancy-code /> @endMacro
Call the macro elsewhere in the same Blade file with the Blade directive or PHP helper:
@showMacro(myMacroName) // or {!! m('myMacroName') !!}
Do you want to use a macro in another Blade view? Use a Blade component.
Change log
Please see the changelog for more information on what has changed recently.
Contributing
Please see contributing.md for details and a todolist.
Credits
License
MIT. Please see the license file for more information.