1

I'm relying on the Low Regex plugin to convert section handles from camelCase to lower-dash-case to use them in URLs, like so:

{section.handle|regex('/(^|[a-z])([A-Z])/e', 'strtolower(strlen("\\1") ? "\\1-\\2" : "\\2")',(section.handle))}

…and further, to convert the segment of the URL that contains the dash-separated section name to camel case, so I can work with it in my templates, like so:

{% set sectionHandle = craft.request.getFirstSegment() %}
{% set sectionHandleCc = sectionHandle | regex('/-([a-z])/e', 'strtoupper(strlen("\\1") ? "\\1\\2" : "\\2")',(sectionHandle)) %}
{% set section = craft.sections.getSectionByHandle(sectionHandleCc) %}

Now, OS X 10.10 Yosemite ships with PHP version 5.5.14 pre-installed and as of 5.5.0 the e-modifier for regex patterns is deprecated, so on my local machine I get this error and the site breaks:

enter image description here

I got in touch with the plugin author and he suggested removing the e-modifier, which seemed to work within the templates, but when I removed the modifier in the sections entry URL format the CP wasn't able to re-save my entries and I couldn't save new entries either.

The question that remains is: Is there a workaround using a different regex pattern or – and that would be my preferred scenario – is there an approach I didn't think of that allows me to omit the plugin dependency completely? I guess one option would be to keep the section handles all lowercase, but I'd like to have them dash-separated in the URLs.

2

Okay, so, thanks to carlcs' answer I managed to solve the camel-case to lower-dash-case conversion. The other direction was a bit trickier, but again, after digging in a bit deeper I was able to figure it out myself:

First off, I'm not using the plugin any longer! Yay! The secret was to create a custom Twig filter that would convert dash-separated strings like so:

{{ 'my-first-car'|dash2camel }}
{# outputs 'myFirstCar' #}

With the help of this guide and some code from this answer I created my own custom Twig filter within Craft. Nothing fancy and hacked together quickly, but I've put it on GitHub if someone runs into the same issues.

1

Can't help you with that \e modifier problem and my regex knowledge is very limited, but that camelCase problem can be solved with Twig filters:

{{ 'camelCase'|replace('/([A-Z])/', '-\\1')|lower }}

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.