Switch in Laravel 5 - Blade

后端 未结 8 522
南旧
南旧 2021-02-02 05:10

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

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


        
相关标签:
8条回答
  • 2021-02-02 05:24

    This is now built in Laravel 5.5 https://laravel.com/docs/5.5/blade#switch-statements

    0 讨论(0)
  • 2021-02-02 05:24

    To overcome the space in 'switch ()', you can use 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 讨论(0)
  • 2021-02-02 05:28

    You can extend blade like so:

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

    You can then use the following:

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

    However do note the warnings in : http://php.net/manual/en/control-structures.alternative-syntax.php

    If there is any whitespace between the switch(): and the first case then the whole code block will fail. That is a PHP limitation rather than a blade limitation. You may be able to bypass it by forcing the normal syntax e.g.:

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

    But this feels a bit wrong.

    0 讨论(0)
  • 2021-02-02 05:31

    You can just add these code in AppServiceProvider class boot method.

    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 as:

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

    Enjoy It~

    0 讨论(0)
  • 2021-02-02 05:35

    IN LARAVEL 5.2 AND UP:

    Write your usual code between the opening and closing PHP statements.

    @php
    switch (x) {
        case 1:
            //code to be executed
            break;
        default:
            //code to be executed
    }
    @endphp
    
    0 讨论(0)
  • 2021-02-02 05:36

    When you start using switch statements within your views, that usually indicate that you can further re-factor your code. Business logic is not meant for views, I would rather suggest you to do the switch statement within your controller and then pass the switch statements outcome to the view.

    0 讨论(0)
提交回复
热议问题