2

I'm trying to use regular expressions with the new template routes in ExpressionEngine 2.8. I have little experience with either.

Using the Template Route Manager, I'd like to route multiple URLs to a single template:

domain.com/workplace-coaching/
domain.com/workplace-coaching/{url-title}
domain.com/personal-coaching/
domain.com/personal-coaching/{url-title}

Problem is, I don't know how to specify segment_1 == 'workplace-coaching' OR segment_1 == 'personal-coaching' using the Template Route Manager.

I'm assuming I need to call upon the Regular Expressions feature so have tried

{variable:regex[(workplace\-coaching|personal\-coaching)]}/{url_title}

Whilst this works, for the urls above, it also seems to match other urls. For instance domain.com/blog now routes to same template as the above urls.

I suspect my Regrex is wrong, and is matching too many options including blog, however don't know how to fix it.

Any insights would be appreciated.

Thank you.

2

The variables need to have a name and that url_title is a reserved word.

To avoid conflicts, if you need to keep the Segments required set to no, keep a static part on the first segment:

/{coaching_type:regex[(workplace|personal)]}-coaching/{coaching_title:alpha_dash}

By this way, you can't grab the first segment by {segment:coaching_type}, but {segment_1} still works.

Please, try this.

| improve this answer | |
  • Hi Robson, Thanks for your suggestion. This isn't working for me, however I might doing something else wrong. I'm assuming I should be able to visit domain.com/workplace-coaching/ and be routed to the correct template. – Andrew F May 21 '14 at 13:48
  • Severity: Warning Message: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 5 Filename: libraries/Template_router.php Line Number: 56 – Andrew F May 21 '14 at 13:55
  • This is a RegEx error, but I just tested with and without a leading /: no errors. Please, check if this bug is related. – Robson Sobral May 21 '14 at 15:10
  • Hi Robson. Amazing. That's got it working perfectly. I also needed to correct the line in the route.php file, as suggested, to fix php errors. Two solutions for the price of one. Many Thanks! – Andrew F May 21 '14 at 19:56
  • @AndrewF, please, check the little explanation I added. Bye! – Robson Sobral May 21 '14 at 20:50

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.