7

Is it possible to provide different possible values per case?

I've tried the following formats without success.

{% case "A" or "B" or "C" %}
  {# run this if case matches A or B or C #}

I saw this done somewhere else so I thought I'd give it a shot

{% case "A" %}
{% case "B" %}
{% case "C" %}
  {# run this if case matches A or B or C #}

From the documentation it doesn't seem like this would be possible but wanted to check just in case. If not, any chance we'll see this sometime in the future?

11

As of Craft 2.6, this is now possible!

In fact, it works exactly like your first example:

{% case "A" or "B" or "C" %}
  {# run this if case matches A or B or C #}

Original answer...

Your first example is not currently possible, but might make for a good feature request.

Your second example is not possible, and most likely never will be. If it were added in the future, that would likely have a major (breaking) impact on all existing uses of the switch statement.

The way that Craft has implemented the case statement is by making it completely unnecessary to supply a break statement. In fact, the break statement doesn't even exist natively (though can be added with the MN Break and Continue plugin). The case statement will automatically end when it hits the next case statement, or the endswitch statement.

An actual solution would be to use a macro or include. If you have multiple case statements which should trigger the same behavior, I'd recommend wrapping that behavior into a macro/include, and calling that macro/include for each relevant case.

  • Thanks Lindsey! I wasn't aware of macros, they'll be a lot of help. And I'll go ahead and submit it as a feature request; I'm sure it'll come in handy in the future. – cballenar Apr 20 '15 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.