Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Matt Stauffer:
Custom conditionals with Laravel's Blade Directives
Oct 12, 2015 @ 14:57:53

Matt Stauffer has a post to his site showing you how to use custom conditionals with Blade, the templating engine for the popular Laravel framework.

One of the greatest aspects of Laravel Blade is that it's incredibly easy to handle view partials and control logic. I find myself frequently extracting the contents of a loop out to a Blade partial and then passing just a bit of data into the partial.

But sometimes the repetitive code isn't the view itself, but the conditional logic I'm running.

He gives a practical example of an issue he had in some recent work he's done: a simple site for his physical trainer that needed to test for public vs non-public resources. The same evaluation code was being pasted all over the site so he looked for a better way. He introduces the idea of a custom directive in Blade and how they're converted to PHP out in the view. He shows how to make an @public directive to replace his cut-and-paste logic from before and make the template much cleaner. He includes the code for this simple directive and ends the post with some other possible options use could use the directives for.

tagged: conditional laravel directive tutorial blade custom evaluation

Link: https://mattstauffer.co/blog/custom-conditionals-with-laravels-blade-directives


Trending Topics: