neosin Posted March 15, 2018 Share Posted March 15, 2018 I haven't test this yet, just trying to pre-strategize before I get the code going When using urlsegments on a multilingual site would I need to include the translated url segment value for the alternate language or will PW handle it? for example in my template if I put if($input->urlSegment1 == 'map') but in the alternate language the segment would be "carte" instead of "map", will PW know by itself that map=carte or carte=map or do I need to take that into consideration in the template also? if($input->urlSegment1 == 'map' || $input->urlSegment1 == 'carte') thanks in advance Link to comment Share on other sites More sharing options...
dragan Posted March 15, 2018 Share Posted March 15, 2018 Why don't you just test it? It's the latter, really, unless I have missed something brand-new lately... Link to comment Share on other sites More sharing options...
Sergio Posted March 15, 2018 Share Posted March 15, 2018 URL segments that you enable on the template settings are just strings, not selectors, so ProcessWire won't be able to do this kind of check. But you can accomplish it by doing the following: On Template Settings, add a regex on the URL Segments field like: regex:^(map|carte)$ On your template code, add a check: // we are only using 1 URL segment, so send a 404 if there's more than 1 if($input->urlSegment2) throw new Wire404Exception(); if($input->urlSegment1) { //if there's any segment that matches the ones you specified on the Template Settings, PW will proceed. //do your code } Going to example.com/map or /carte will pass the conditional. 3 Link to comment Share on other sites More sharing options...
neosin Posted March 15, 2018 Author Share Posted March 15, 2018 1 minute ago, Sergio said: URL segments that you enable on the template settings are just strings, not selectors, so ProcessWire won't be able to do this kind of check. But you can accomplish it by doing the following: On Template Settings, add a regex on the URL Segments field like: regex:^(map|carte)$ On your template code, add a check: // we are only using 1 URL segment, so send a 404 if there's more than 1 if($input->urlSegment2) throw new Wire404Exception(); if($input->urlSegment1) { //if there's any segment that matches the ones you specified on the Template Settings, PW will proceed. //do your code } Going to example.com/map or /carte will pass the conditional. thank you for the clear example, I was not even aware of the template regex possibility until this moment, mind you I've only been playing with PW for 2 days. This is great news and means less coding required and no changes of template code if things change down the road in the project. thanks! Link to comment Share on other sites More sharing options...
Sergio Posted March 15, 2018 Share Posted March 15, 2018 No problem! Regex to the rescue! Take a look at other examples here: https://processwire.com/docs/admin/setup/templates/#which-url-segments-do-you-want-to-allow 1 Link to comment Share on other sites More sharing options...
Sergio Posted March 15, 2018 Share Posted March 15, 2018 Came back to say that the regex example I gave can also work without regex, because we're not actually using its power. So, if you just add "map" and "carte", one per line, on the URL Segment field, in the template settings, it will also work. Regex is more appropriate when you have cases like these: //to get segments like /export-json or /export-xml regex:^export-(json|xml)$ //to get segments like "2018/episodes" regex:^\d{4}/episodes$ //to get segments like "category/ebooks/". I used this when there's no category pages associated with the template, just options of a Options field. regex:^category/[a-z]+$ 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now