Laravel: How to use ternary operators in Blade templates

Updated: January 18, 2024 By: Guest Contributor Post a comment

Introduction

When developing web applications with Laravel, you might often come across situations where you want to conditionally display data or choose between two options directly in your Blade templates. This can lead to code that is more verbose and harder to read. However, Laravel’s Blade templating engine provides a way to simplify these conditional statements using ternary operators. In this tutorial, we will cover the basics of ternary operators and how to use them effectively in your Blade templates.

Understanding Ternary Operators

A ternary operator is a concise way to perform an if-else statement in just one line of code. The basic syntax of a ternary operator is:

<condition> ? <value if true> : <value if false>;

This can be read as: “if the condition is true, use the value before the colon, otherwise, use the value after the colon.”

Basic Usage in Blade

In Blade templates, ternary operators are used with the same syntax as PHP. Consider the following example where you want to assign a default value to a variable if it’s not set:

{{ $title ?? 'Default Title' }}

This example uses the PHP null coalescing operator (??) which is a shorthand ternary operator that checks if the left operand is null, and if so, it returns the right operand.

Compare with Conditional Statements

Without the ternary operator, the same operation would look like this:

@if(isset($title)) {{ $title }} @else Default Title @endif

As you can see, the ternary operator allows us to write the condition in a highly compressed form.

Inline Conditional Display

Sometimes you just want to display text or execute some code based on a condition. In those cases, you can use the syntax as follows:

{{ $isLoggedIn ? 'Logout' : 'Login' }}

This will display ‘Logout’ if $isLoggedIn is true, and ‘Login’ if it is not.

Using Ternary Operators with Methods

If you need to check the result of a method, the syntax remains the same. Here’s an example of how you might display a user’s role:

{{ Auth::user()->isAdmin() ? 'Administrator' : 'User' }}

Nested Ternary Operators

It is also possible to nest ternary operators for more complex conditions, though readability might be a concern. When doing this, it’s essential to use parentheses to clarify the order of operations:

{{ ($score >= 90) ? 'A' : (($score >= 80) ? 'B' : 'C') }}

This line of code will return ‘A’ if $score is greater than or equal to 90, ‘B’ if it’s between 80 and 89, and ‘C’ for anything less.

Alternative to Nested Ternary Operators

Nesting ternary operators can quickly become difficult to read and maintain. An alternative is to use @php directive to set a variable, achieving the same result with better readability:

@php
    $result = ($score >= 90) ? 'A' : (($score >= 80) ? 'B' : 'C');
@endphp

{{ $result }}

Conclusion

Using ternary operators in Blade can simplify your templates and allow you to write clean, concise conditional statements. However, it’s important to balance the need for brevity with the need for clear and maintainable code. Avoid nested ternary operators when possible and always consider if the ternary operator is the best choice for the clarity of your code. Remember, more compact code isn’t always better if it sacrifices readability.

As a best practice, prefer the use of ternary operators for simple conditions and default value assignments. For more complex logic, consider using the traditional @if directives or handling the logic in your controller or model to keep your Blade templates as clean and readable as possible.