Conditional Classes Blade Directives in Laravel

New in Laravel 8.51: @class Blade directive to add true/false conditions on whether some CSS class should be added.

The @class directive conditionally compiles a CSS class string. The directive accepts an array of classes where the array key contains …


This content originally appeared on DEV Community and was authored by Suresh Ramani

New in Laravel 8.51: @class Blade directive to add true/false conditions on whether some CSS class should be added.

The @class directive conditionally compiles a CSS class string. The directive accepts an array of classes where the array key contains the class or classes you wish to add, while the value is a boolean expression. If the array element has a numeric key, it will always be included in the rendered class list:

@php
    $hasError = true;
@endphp

<span @class([
    'p-4',
    'bg-red' => $hasError,
])></span>

Thank you for reading this blog.


This content originally appeared on DEV Community and was authored by Suresh Ramani


Print Share Comment Cite Upload Translate Updates
APA

Suresh Ramani | Sciencx (2021-07-28T21:49:03+00:00) Conditional Classes Blade Directives in Laravel. Retrieved from https://www.scien.cx/2021/07/28/conditional-classes-blade-directives-in-laravel/

MLA
" » Conditional Classes Blade Directives in Laravel." Suresh Ramani | Sciencx - Wednesday July 28, 2021, https://www.scien.cx/2021/07/28/conditional-classes-blade-directives-in-laravel/
HARVARD
Suresh Ramani | Sciencx Wednesday July 28, 2021 » Conditional Classes Blade Directives in Laravel., viewed ,<https://www.scien.cx/2021/07/28/conditional-classes-blade-directives-in-laravel/>
VANCOUVER
Suresh Ramani | Sciencx - » Conditional Classes Blade Directives in Laravel. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/28/conditional-classes-blade-directives-in-laravel/
CHICAGO
" » Conditional Classes Blade Directives in Laravel." Suresh Ramani | Sciencx - Accessed . https://www.scien.cx/2021/07/28/conditional-classes-blade-directives-in-laravel/
IEEE
" » Conditional Classes Blade Directives in Laravel." Suresh Ramani | Sciencx [Online]. Available: https://www.scien.cx/2021/07/28/conditional-classes-blade-directives-in-laravel/. [Accessed: ]
rf:citation
» Conditional Classes Blade Directives in Laravel | Suresh Ramani | Sciencx | https://www.scien.cx/2021/07/28/conditional-classes-blade-directives-in-laravel/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.