Using macros in Laravel Blade

I stumbled across this by chance, this approach was one I often sought in Twig templates and wanted to try my hand at advanced Blade templates.

Lets say for a second that you have a complex form input, maybe one that contains a lot of divs and conditionals on what to display. Attached is the example I was working with:

This could create the "copy-and-paste" condition that we, as good developers, should avoid.

So whats the best approach to solve this? My first idea was Form Macros. I won't cover this as Jeffery Way provides an excellent tutorial on Laracasts, you can watch here - https://laracasts.com/lessons/form-macros-for-the-win.

The second approach, which was one I felt more comfortable with, was to use Blade itself. Before I started, I looked at the common elements of the form and came up with:

label labelFor name id *class

So I created a new directory named "resources/views/_macros". Within the directory I created a new file named "file.blade.php" and pasted the following code:

Right off the bat I had few concerns, first I was running a conditional against a Blade variable… I did not bother to dig into the parsing order of Blade because well, if it broke it broke… That would be my indicator. So I proceeded to remove a working field from the site and using @include, I passed the required variables, like so:

So I reloaded the page with my fingers crossed and to my surprise… IT WORKED! So I uploaded a file, added a little extra to fluff it up and was genuinely amazed.

I was always a little skeptical of using Blade but the more I dive into it, the more impressed I am.