Blade inline if and else if statement

Is there any syntax for specifying inline if and else if in a Laravel blade server template?

Usually the syntax for the if and else expression is:

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

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

 {{ $var === "hello" ? "Hi" : "Goodbye" else if $var ==="howdie ? "how" : "Goodbye""}}
+15
source share
5 answers

You can use this code in a long blade:

{{  $var === "hello" ? "Hi" : ($var ==="howdie ? "how" : "Goodbye") }}
+29
source

remember that not every short code is good. in your example there is no one way to get into this else ifbecause you say

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"; }

Wrong, you cannot use two elseaccordingly. you have a structure for your conditions, such as

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

}

same thing in triple operators

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

(, ) .

, , , . if else if, .

+4
{{ ($var === "hello") ? "Hi" : ($var ==="howdie") ? "how" : "Goodbye""}}

Try mario !

+1
source

I believe that these are two if else statements on the same line. I cannot imagine how to make it inline, but I would do something like this.

@if($var=="hello" || $var=="Hi")
   {{$var === "hello" ? "Hi" : "Howdie"}}
@else
   {{"Goodbye"}}
@endif
0
source
<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 @ not selected yet @endif

0
source

Source: https://habr.com/ru/post/1016045/


All Articles