Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] Switch block for Volt #13107

Closed
lajosbencz opened this issue Oct 3, 2017 · 12 comments
Closed

[NFR] Switch block for Volt #13107

lajosbencz opened this issue Oct 3, 2017 · 12 comments
Assignees
Milestone

Comments

@lajosbencz
Copy link

New feature request, as per: https://forum.phalconphp.com/discussion/17035/feature-request-switch-for-volt

Intended behaviour

Allows for switch/case conditional rendering in Volt

Example

{% switch matrixBlock.type %}

 {% case "text" %}

     {{ matrixBlock.textField | markdown }}

 {% case "image" %}

    {{ matrixBlock.image[0].getImg() }}

 {% default %}

    <p>A font walks into a bar.</p>
    <p>The bartender says, “Hey, we don’t serve your type in here!</p>

{% endswitch %}
@JABirchall
Copy link

JABirchall commented Oct 3, 2017

Good suggestion Would be great for example a role badge. But would need a way to break out the switch as well so that it doesn't cascade. I suggest a {% casebreak 'match' %}

{% switch user.getRoles() %}
    {% case "Owner" %}
    <span class="label label-pill label-primary">Owner</span>
    {% case "Admin" %}
     <span class="label label-pill label-primary">Admin</span>
    {% casebreak "Partner" %} // this should break the switch if matched
    <span class="label label-pill label-primary">Partner</span>
    {% default %}
    Member
{% endswitch %}

@Slyvampy
Copy link

Slyvampy commented Oct 6, 2017

Thanks for creating this NFR for me :)

@sergeyklay
Copy link
Contributor

sergeyklay commented Oct 17, 2017

Actually I prefer more something like:

{% switch username %}
    {# Each `case` statement can take multiple values to compare the variable #}
    {% case "Jim" "Bob" "Joe" %}
        Hello {{ username }}
    {% case "Nik" %}
        Hey hey {{ username }}!
    {% default %}
        {#
            An optional `default` statement sets off the default output
            if no matches could be found
        #}
        Who are you?
{% endswitch %}

I need to investigate how to do this with break-statements support. Also I think it should be endcase to follow common language design: endfor, endif, endblock, endswitch, etc.

@Jurigag
Copy link
Contributor

Jurigag commented Oct 17, 2017

Imho there shouldn't be endcase. Break should be just detected by next {% case or {% default.

Also not sure about this multiple values for case. Is it possible in plain php? I don't think so, not sure if i saw syntax like this in any language/template. But it might be good thing.

@OMSmolina
Copy link

@Jurigag You "can" more or less with true/false.

<?php
        $value = 'Nik';
        switch (true) {
            case ($value === 'Jim' || $value === 'Bob' || $value === 'Joe'): {
                echo 'Hello Multiple';
            }
            break;
            case ($value === 'Nik') : {
                echo 'Hello Nik';
            }
            break;
            default: {
                echo 'Hello nothing';
            }
        }

@JABirchall
Copy link

JABirchall commented Oct 18, 2017

@oscarmolinadev but that is contradictory, you might as well generate a series of if statements. the switch() is omitted how you are using it.

Its like doing a while(true) {} and then checking if you should break out the while loop inside of it. its something you should never do.

switch cases have the functionality to have multiple condition match for the same code block

switch($value) {
    case 'jim':
    case 'bob':
    case 'joe':
        echo 'Hello Multiple';
        break;
    case 'nik':
        echo 'Hello Nik';
        break;
    default:
        echo 'Hello anybody';
}

@OMSmolina
Copy link

@JABirchall Totally agree with you! My example is what you never should do. I see this ugly sh*** in my work one time. That's why I've put "can" :) In PHP you can make ugly things like that.. and while(true){} makes no sense in PHP either.

@sergeyklay
Copy link
Contributor

I'm almost done worked version for php5 (without break still) so I need few days for php7

@JABirchall
Copy link

I see this ugly sh*** in my work one time.

@oscarmolinadev Yea, if i see code like that in a PR at work I rip them a new one. It's not our policy to be rude to other employees when they do dumb shit like that but I just can't help my self xD. its something that infuriates me.

@Jurigag
Copy link
Contributor

Jurigag commented Oct 19, 2017

@oscarmolinadev this is not purpose of switch really. For that you have if construction. Also there is in_array function.

@sergeyklay
Copy link
Contributor

sergeyklay commented Oct 20, 2017

Latest news: I have worked version for switch-case-break-default-endswitch for PHP 5

@sergeyklay
Copy link
Contributor

Implemented in 3.3.x branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants