Switch to Laravel 5 - Blade

How can I use the switch in blade templates? When I used:

@switch($login_error) @case(1) `E-mail` input is empty! @break @case(2) `Password` input is empty! @break @endswitch 

As a result, I see this text as plain text. I prefer to use the switch in several parts of the code because it is cleaner for me than when I use if.

But if this is not possible, just write.

+50
php switch-statement laravel laravel-5 blade
Apr 27 '15 at 13:42 on
source share
8 answers

Unfortunately, Laravel Blade does not have a switch statement. You can use Laravel if a different approach or use uses a simple PHP switch. You can use simple PHP in blade templates, as in any other PHP application. Starting with Laravel 5.2 and higher, use the @php statement.

Option 1:

 @if ($login_error == 1) `E-mail` input is empty! @elseif ($login_error == 2) `Password` input is empty! @endif 

Option 2:

 @php switch ($login_error) { case 1: echo "`E-mail` input is empty!"; break; case 2: echo "`Password` input is empty!"; } @endphp 

Option 3:

 <?php switch ($login_error) { case 1: echo "`E-mail` input is empty!"; break; case 2: echo "`Password` input is empty!"; } ?> 
+55
Apr 27 '15 at 13:45
source share

You can simply add this code to the loading method of the AppServiceProvider class.

 Blade::extend(function($value, $compiler){ $value = preg_replace('/(\s*)@switch\((.*)\)(?=\s)/', '$1<?php switch($2):', $value); $value = preg_replace('/(\s*)@endswitch(?=\s)/', '$1endswitch; ?>', $value); $value = preg_replace('/(\s*)@case\((.*)\)(?=\s)/', '$1case $2: ?>', $value); $value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value); $value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '<?php break;', $value); return $value; }); 

then you can use it like:

 @switch( $item ) @case( condition_1 ) // do something @breakswitch @case( condition_2 ) // do something else @breakswitch @default // do default behaviour @breakswitch @endswitch 

Enjoy it ~

+25
Feb 24 '16 at 15:36
source share

In LARAVEL 5.2 AND UP:

Write your regular code between PHP open and close operations.

 @php switch (x) { case 1: //code to be executed break; default: //code to be executed } @endphp 
+20
Sep 09 '16 at 14:55
source share

In Laravel 5.1, this works in Blade:

 <?php switch( $machine->disposal ) { case 'DISPO': echo 'Send to Property Disposition'; break; case 'UNIT': echo 'Send to Unit'; break; case 'CASCADE': echo 'Cascade the machine'; break; case 'TBD': echo 'To Be Determined (TBD)'; break; } ?> 
+5
Nov 02 '16 at 14:16
source share
+2
Sep 20 '17 at 14:12
source share

You can stretch the blade like this:

  Blade::directive('switch', function ($expression) { return "<?php switch($expression): ?>"; }); Blade::directive('case', function ($expression) { return "<?php case $expression: ?>"; }); Blade::directive('break', function () { return "<?php break; ?>"; }); Blade::directive('default', function () { return "<?php default: ?>"; }); Blade::directive('endswitch', function () { return "<?php endswitch; ?>"; }); 

Then you can use the following:

 @switch($test) @case(1) Words @break @case(2) Other Words @break @default Default words @endswitch 

However, pay attention to the warnings at: http://php.net/manual/en/control-structures.alternative-syntax.php

If there are spaces between the switch (): and the first case, then the entire code block will fail. This is a limitation of PHP, not a limitation of the blade. You can get around it by calling the normal syntax, for example:

 Blade::directive('switch', function ($expression) { return "<?php switch($expression) { ?>"; }); Blade::directive('endswitch', function ($) { return "<?php } ?>"; }); 

But this is a little wrong.

0
Jun 20 '17 at 10:17
source share

To bridge the gap in 'switch ()', you can use the code:

 Blade::extend(function($value, $compiler){ $value = preg_replace('/(\s*)@switch[ ]*\((.*)\)(?=\s)/', '$1<?php switch($2):', $value); $value = preg_replace('/(\s*)@endswitch(?=\s)/', '$1endswitch; ?>', $value); $value = preg_replace('/(\s*)@case[ ]*\((.*)\)(?=\s)/', '$1case $2: ?>', $value); $value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value); $value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '<?php break;', $value); return $value; }); 
0
Jun 26 '17 at 5:10
source share

When you start using switch statements in your views, this usually indicates that you can change your code again. The business logic is not intended for representations, I would prefer to offer you to make a switch statement inside your controller, and then pass the result of the switch statement to the view.

-four
Apr 27 '15 at 14:10
source share



All Articles