Switch in Laravel 5 - Blade

后端 未结 8 534
南旧
南旧 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:28

    You can extend blade like so:

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

    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 "";
    });
    Blade::directive('endswitch', function ($) {
        return "";
    });
    

    But this feels a bit wrong.

提交回复
热议问题