[NFR] Switch block for Volt
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 %}
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 %}
Thanks for creating this NFR for me :)
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.
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 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 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 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.
I'm almost done worked version for php5 (without break still) so I need few days for php7
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.
@oscarmolinadev this is not purpose of switch really. For that you have if construction. Also there is in_array function.
Latest news: I have worked version for switch-case-break-default-endswitch for PHP 5
Implemented in 3.3.x branch