0

Trying to get switchee to work with a regex check against a URL segment.

For example following URL check if segment 2 is numeric:

/property/123

Using:

{exp:switchee variable = "{segment_2}"}
    {case value="#[0-9]+#"}
        IS A NUMBER
    {/case}
{/exp:switchee} 

However this isn't working. Any thoughts/suggestions?

Cheers

Cole

1

You just need to set the parse parameter to inward if you want to discard the content of the unmatched cases before parsing them or you are using a module or plugin tag as a parameter.

The RegEx you're using, /[0-9]+/, will match strings containing a number, not being one.

Please, try:

{exp:switchee variable="{segment_2}" parse="inward"}
    {case value="#^\d+$#"}
        IS A NUMBER
    {/case}
    {case default="yes"}
        NOT A NUMBER
    {/case}
{/exp:switchee}

^\d+$ will match just numbers.

2
  • Thanks - this seems to have worked when the switchee condition is at root but not when using nested conditions. Any thoughts why that might be? Oct 9 '14 at 8:55
  • I can't imagine a reason for this. Can you show me some code?
    – Sobral
    Oct 9 '14 at 12:44
0

Add parse="inward" to switchee:

{exp:switchee variable="{segment_2}" parse="inward"}
    {case value="#[0-9]+#"}
        IS A NUMBER
    {/case}
    {case default="yes"}
        NOT A NUMBER
    {/case}
{/exp:switchee}

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.