Blade inline if and else if statement
Asked Answered
D

6

42

Is there a syntax to specify inline if and else if statement in Laravel blade template?

Normally, the syntaxt for if and else statement would be :

{{ $var === "hello" ? "Hi" : "Goodbye" }}

I would now like to include else if statement, is this possible?

 {{ $var === "hello" ? "Hi" : "Goodbye" else if $var ==="howdie ? "how" : "Goodbye""}}
Disfigurement answered 26/3, 2017 at 4:44 Comment(1)
What you want is a nested ternary. @mopo 's answer is the right answer. But nested ternaries are undesirable, they are hard to read and if you come by this code in 3 months it wil not be immediately evident what you were trying to do. Put the logic in the controller.Voltcoulomb
D
80

You can use this code in laravel blade:

{{  $var === "hello" ? "Hi" : ($var ==="howdie ? "how" : "Goodbye") }}
Demi answered 26/3, 2017 at 7:37 Comment(4)
What if I'm going to add HTML tag inside? Example is '<h1>Goodbye</h1>'. It display the tag.Vivianviviana
Then you use {!! code here !!} as open and close tags instead of {{ code here }}Sold
{{ $var === "hello" ? "Hi" : ($var ==="howdie" ? "how" : "Goodbye") }} missing double quote.Koosis
This is a very bad idea imo, completely unreadabe, take the several lines, do the @if and the @else - you'll thank me in a few years when you're editing your own code.Fellers
A
8

remember not every short code is a good one. in your example there's no single way to hit this else if because you're saying

if($var === "hello")
    { 
        // if the condetion is true
        "Hi";
    }
else
    { 
        // if the condetion is false
        "Goodbye";
    }
// error here
else if($var ==="howdie")
    { "how"; }
else
    { "Goodbye"; }

this's wrong you can't use two elses respectively. you've structure your conditions like

if (condition) {
    # code...
} elseif (condition) {
    # code...
} else {

}

the same in the ternary operators

(condition) ? /* value to return if first condition is true */ 
: ((condition) ? /* value to return if second condition is true */ 
: /* value to return if condition is false */ );

and beware of (,) in the second condition.

and as you see your code is just going to be tricky, unreadable and hard to trace. so use the if else if if you've more than one condition switching and revise your logic.

Arouse answered 26/3, 2017 at 6:28 Comment(0)
H
6
<select id="days" class="Polaris-Select__Input" name="days" aria-invalid="false">
    <option value="10" @if($settingsData->days == "10") selected @endif >at 10 Days</option>
</select>

@if($settingsData->days == "10") selected @else not selected @endif

Horton answered 4/9, 2019 at 4:38 Comment(1)
this produces extra spacesVegetative
I
0

I believe that is two if else statements in one line. I cant imagine way to make it inline but i would have done something like this.

@if($var=="hello" || $var=="Hi")
   {{$var === "hello" ? "Hi" : "Howdie"}}
@else
   {{"Goodbye"}}
@endif
Instance answered 26/3, 2017 at 4:53 Comment(0)
D
0

with this code you can write single line if-else laravel blade with four condition.

{
  {
    $a == 10
      ? "10"
      : $a == 20
      ? "20"
      : $a == 30
      ? "30"
      : $a == 40
      ? "40"
      : "nothing";
  }
}
Dubbin answered 3/5, 2022 at 19:8 Comment(0)
D
0
$pandit->pandit_id != (auth()->user() ? "not pandit" : (auth()->guard('pandit')->user() ? auth()->guard('pandit')->user()->id : "vendor"
Dumas answered 9/7, 2022 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.