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

Comments

Projects
None yet
6 participants
@lajosbencz

lajosbencz commented Oct 3, 2017

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

This comment has been minimized.

Show comment
Hide comment
@JABirchall

JABirchall 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 %}

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

This comment has been minimized.

Show comment
Hide comment
@Slyvampy

Slyvampy Oct 6, 2017

Thanks for creating this NFR for me :)

Slyvampy commented Oct 6, 2017

Thanks for creating this NFR for me :)

@sergeyklay

This comment has been minimized.

Show comment
Hide comment
@sergeyklay

sergeyklay Oct 17, 2017

Member

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@Jurigag

Jurigag Oct 17, 2017

Member

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.

Member

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.

@oscarmolinadev

This comment has been minimized.

Show comment
Hide comment
@oscarmolinadev

oscarmolinadev Oct 18, 2017

@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';
            }
        }

oscarmolinadev commented Oct 18, 2017

@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

This comment has been minimized.

Show comment
Hide comment
@JABirchall

JABirchall 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';
}

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';
}
@oscarmolinadev

This comment has been minimized.

Show comment
Hide comment
@oscarmolinadev

oscarmolinadev Oct 18, 2017

@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.

oscarmolinadev commented Oct 18, 2017

@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

This comment has been minimized.

Show comment
Hide comment
@sergeyklay

sergeyklay Oct 18, 2017

Member

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

Member

sergeyklay commented Oct 18, 2017

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

@sergeyklay sergeyklay added the Volt label Oct 18, 2017

@JABirchall

This comment has been minimized.

Show comment
Hide comment
@JABirchall

JABirchall Oct 19, 2017

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.

JABirchall commented Oct 19, 2017

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

This comment has been minimized.

Show comment
Hide comment
@Jurigag

Jurigag Oct 19, 2017

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@sergeyklay

sergeyklay Oct 20, 2017

Member

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

Member

sergeyklay commented Oct 20, 2017

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

@sergeyklay

This comment has been minimized.

Show comment
Hide comment
@sergeyklay

sergeyklay Oct 22, 2017

Member

Implemented in 3.3.x branch

Member

sergeyklay commented Oct 22, 2017

Implemented in 3.3.x branch

@sergeyklay sergeyklay closed this Oct 22, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment