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

Laravel News:
Five Useful Laravel Blade Directives
May 23, 2018 @ 14:29:53

On the Laravel News site they've shared a list of five useful Blade directives that you may not have known about. Blade is the templating language included with the Laravel framework by default.

We’re going to look at five Laravel Blade directives you can use to simplify your templates, and learn about some convenient directives that make solving specific problems a cinch! If you’re new to the framework, these tips will help you discover the excellent features of Blade, Laravel’s templating engine.

The five on their list provide functionality to:

  1. Check if the user is authenticated
  2. Check if the user is a guest
  3. Include the first view if it exists or includes the second if it doesn’t
  4. Include a view based on a condition
  5. Include a view if it exists

Each one comes with example code showing it in use and a bit of explanation behind its use.

tagged: laravel directive blade template top5 list

Link: https://laravel-news.com/five-useful-laravel-blade-directives

Laravel News:
Appstract Laravel Blade Directives
Oct 04, 2017 @ 15:22:54

In a new post to the Laravel News site they highlight a package that adds a set of common Blade directives to your Laravel application.

With no shortage of excellent blade extensions circulating in the community, you should also check out the appstract/laravel-blade-directives package.

Whether you end up installing the package or copying a few that you like to your project, here are some notable directives from the project.

Directives included in the package can be used to: check the current route for a match, see if a value is true or not, inline the contents of a CSS file and many more. Check out the full post for examples of these and others the package includes. The source for the package can be found in this repository on GitHub.

tagged: laravel blade directive custom package tutorial

Link: https://laravel-news.com/appstract-laravel-blade-directives

INANI El Houssain:
Build your OWN switch statment using Laravel’s custom blade directives
Nov 03, 2016 @ 15:26:01

In this post on his Medium blog INANI El Houssain shows you how to create a custom directive for use with Laravel's Blade templating language. In this example he shows how to make a custom switch statement, something commonly used on the PHP side to select an action based on a value.

One of the good points of Laravel’s framework is that it allows you to make your own components, macros and directives. so today we will make use of Laravel’s Custom Blade directives and make something good.

He starts with a simple "hello world" example to show where the pieces all live, outputting a simple "Hello $name" string. He then moves into the creation of the "@switch" directive having it write out the PHP code required for the switch to start and end. He adds in two more tags to start and end the different cases: @case and @endcase. The post wraps up with an example of all of these tags in use and how to catch when the value under evaluation might be empty.

tagged: laravel blade directive custom output switch tutorial case

Link: https://medium.com/@InaniT0/build-your-own-switch-statment-using-laravels-custom-blade-directives-218244e41a7c#.dtkbzif3j

Pascal Martin:
INI directives are evil!
Apr 28, 2016 @ 17:58:40

In a new post to his site Pascal Martin shares some thoughts about why INI directives are evil, mostly in how they could be used to enable/disable major pieces of functionality in the PHP language.

A few times, while evolutions were discussed for PHP 7, someone suggested a new feature could be optional, depending on an INI configuration directive — the idea being each user could then enable it or not.

Still, the idea of directives that could change, sometimes deeply, the behavior of a programming language… It scares me!

He goes back in time a bit to talk about a feature like this that was once a part of the language (happily removed now): "magic quotes". He points out that, while the intent was to provide security to submitted data, the results were disastrous if it was moved to another server without the setting enabled. He also points out some of the steps that have to be taken when a new directive controlling a major feature is introduced - even worse if you're creating a product to run on other peoples' servers.

In any case, before suggesting "but they could allow us to enable or not this feature with a simple INI directive" for ideas as critical as a weak or strict typing mechanism, ask yourself: do you really want two languages with very distinct behaviors, and applications and libraries that work only on some combinations of configuration values?
tagged: ini directive feature language evil opinion

Link: https://blog.pascal-martin.fr/post/ini-directives-are-evil.html#fn:why-optionnal-feature

Zaengle Blog:
Exploring Laravel's Custom Blade Directives
Mar 14, 2016 @ 18:38:30

On the Zaengle blog there's a post spotlighting the custom Blade directive functionality that comes with using the Blade templating engine of the Laravel framework. These allow the definition of custom functionality available directly from the templating layer.

Earlier today, I was working on coding up a design that displays a varying number of cards - each with a unique title and description… think Masonry/Pinterest-esque. I’ve been using Model Factories to stub out a bunch of cards, all with different content. Once I’d hooked up the dummy data to the card templates, I realized that the design didn’t work as well with titles that had more than 20 or so characters.

A common solution to this would be to use CSS to break the line and automatically add an ellipsis. [...] However, this wouldn’t work well in my situation because the design allows titles to be two lines long. Another solution would be to chop off the title at a given length and add an ellipsis using a php snippet. [...] However, adding this much PHP to my Blade templates would have really mucked up my otherwise-clean templates. So I decided to drop this functionality into a custom Blade directive that I could reuse where necessary.

He shows how to define the custom Blade directive in the AppServiceProvider class (autoloaded with every request) for a simple "Hello World" example. He also shows how to use this in the template code, making a simple call to its matching helloWorld tag. He then implements his custom truncate handling, returning some simple PHP code that automatically reduces the content down to a given length and echoes out the result.

tagged: laravel custom blade directive tutorial helloworld truncate

Link: http://zaengle.com/blog/exploring-laravels-custom-blade-directives

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: