New `@bool` Blade directive in Laravel!

Susan Sarandon
Release: 2024-10-21 06:07:30
Original
919 people have browsed it

New `@bool` Blade directive in Laravel!

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:

<script>
    let config = {
        isActive: @bool($isActive),
        hasAccess: @bool($hasAccess)
    };
</script>
Copy after login

When compiled, this Blade code will output clean JavaScript:

<script>
    let config = {
        isActive: true,
        hasAccess: false
    };
</script>
Copy after login

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:

<button aria-haspopup="@bool($hasPopup)" aria-expanded="@bool($isExpanded)">
    Dropdown button
</button>
Copy after login

Availability & PR

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. In the meantime, check out the merged PR on GitHub! https://github.com/laravel/framework/pull/53179

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

The above is the detailed content of New `@bool` Blade directive in Laravel!. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!