Skip to content

New `@bool` Blade directive in Laravel

Posted by author

The new @bool Blade directive

Laravel's Blade templating engine is getting a handy new feature: the @bool directive. This allows you to directly print boolean values into strings or use them in object construction, making your JavaScript integrations cleaner and more efficient.

Here's how you can use it:

1<script>
2 let config = {
3 isActive: @bool($isActive),
4 hasAccess: @bool($hasAccess)
5 };
6</script>

When compiled, this Blade code will output clean JavaScript:

1<script>
2 let config = {
3 isActive: true,
4 hasAccess: false
5 };
6</script>

Use cases

The @bool directive is particularly useful in several scenarios:

  1. JavaScript configuration objects
  2. Alpine.js data binding
  3. HTML attributes requiring boolean values

For instance, with Bootstrap:

1<button aria-haspopup="@bool($hasPopup)" aria-expanded="@bool($isExpanded)">
2 Dropdown button
3</button>

Availability

While this feature isn't released yet, it's expected to be available soon. Keep an eye on Laravel's official channels for the announcement.

Conclusion

The @bool directive is a small but powerful addition to Blade that will make working with boolean values in your templates much more convenient.

If any part of this post was helpful, please let me know and give me a follow on Twitter/X where I'm @CodeWithCaen


Syntax highlighting by Torchlight.dev

End of article