cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Switch block for Volt

Open lajosbencz opened this issue 9 years ago • 12 comments

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

lajosbencz avatar Oct 03 '17 10:10 lajosbencz

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 avatar Oct 03 '17 15:10 JABirchall

Thanks for creating this NFR for me :)

Slyvampy avatar Oct 06 '17 10:10 Slyvampy

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.

sergeyklay avatar Oct 17 '17 19:10 sergeyklay

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.

Jurigag avatar Oct 17 '17 20:10 Jurigag

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

OMSmolina avatar Oct 18 '17 07:10 OMSmolina

@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 avatar Oct 18 '17 11:10 JABirchall

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

OMSmolina avatar Oct 18 '17 12:10 OMSmolina

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

sergeyklay avatar Oct 18 '17 12:10 sergeyklay

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 avatar Oct 19 '17 01:10 JABirchall

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

Jurigag avatar Oct 19 '17 06:10 Jurigag

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

sergeyklay avatar Oct 20 '17 14:10 sergeyklay

Implemented in 3.3.x branch

sergeyklay avatar Oct 22 '17 01:10 sergeyklay