heldercervantes Posted May 8, 2015 Share Posted May 8, 2015 I have a couple of problems regarding frontend template management and hacking the page tree. Imagine a catalogue that acts as a compilation of various lists of products from multiple users. Looking at the page tree, I have something like this: home -- products ------ toy ------ vase ------ phone -- users ------ john ------ jane ------ mary -- search -- ... Problem #1 : Direct user page links. Although I'm grouping users under the "users" page, I'd like to go around it and generate links that skip the users page, like so: www.site.com/users/john (bad) www.site.com/john (good) Problem #2 : Same product (page), two places, two frontend templates. Visitors can reach the main site, search and find products regardless of the user who added them, or they can go straight in a user's page and browse their specific catalogue. I'd even like to offer the option of having "John's" domain point straight into "www.site.com/john". The problem here is that I want the same product to be represented with a different template in each case. www.site.com/products/toy (main site template) www.site.com/john/toy (user specific template with extra menus and different look) I'm thinking maybe I need to place products inside each user's page. That may organize and simplify a bit, but I'll still have the same problem of using a different frontend template when users search from the main site. Any ideas? Thanks, HC Link to comment Share on other sites More sharing options...
mr-fan Posted May 8, 2015 Share Posted May 8, 2015 One link on this topic but no expirience with pagetree/url hacking: https://processwire.com/talk/topic/5448-remove-parents-from-url/ best regards mr-fan Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 8, 2015 Share Posted May 8, 2015 Regarding Problem #2: I think the best way would be to store product information in a separate part of the tree, without frontend access and then use the data from there in all the places you need them. For both problems I'd suggest you should take a look at urlSegments. 2 Link to comment Share on other sites More sharing options...
OLSA Posted May 8, 2015 Share Posted May 8, 2015 Hello for all, not sure can my suggestion help (I am still new in PW world ), but had experiences in other systems. When I need to create multi-relations I also use tagging ("tags"). In that case you get new relations (eg. have categories, but categories + tags can give you almost unlimited options). Maybe you can try that option? regards, Sasa 1 Link to comment Share on other sites More sharing options...
diogo Posted May 8, 2015 Share Posted May 8, 2015 Building on LostKobrakai's suggestion, you could do it like this: #1 Enable urlSegments on the home template https://processwire.com/docs/tutorials/how-to-use-url-segments/ Then, on the home template, you'll want to find the user by the provided segment: if ($input->urlSegment1) { $cleanUser = $sanitizer->pageName($input->urlSegment1); // sanitize user input $foundUser = $pages->get("parent=/users/, name=$cleanUser"); // find the requested user page if (!$foundUser->id) throw new Wire404Exception(); // throw a 404 if there isn't that user echo $foundUser->render(); // render the user page } else { // code for the homepage } The problem here is that if you have a page that is children of home with the same name as a user, that page will be called and the segment will be ignored. Make sure to check the best practices for working with url segments here: https://processwire.com/docs/tutorials/how-to-use-url-segments/page3 #2 You could still keep the products under /products/ and have a second urlSegment on the homepage. The logic would be the same as in #1. So, all together (written in the browser, don't trust the code too much): if($input->urlSegment3) throw new Wire404Exception(); // we don't care for a third segment, so throw a 404 if it exists if ($input->urlSegment1) { // first check the user $cleanUser = $sanitizer->pageName($input->urlSegment1); // sanitize input $foundUser = $pages->get("parent=/users/, name=$cleanUser"); // find the requested user page if (!$foundUser->id) throw new Wire404Exception(); // throw a 404 if there isn't that user if ($input->urlSegment2) { // check the product $cleanProduct = $sanitizer->pageName($input->urlSegment2); // sanitize input $foundProduct = $pages->get("parent=/products/, name=$cleanProduct"); // find the requested product page if (!$foundProduct->id) throw new Wire404Exception(); // throw a 404 if there isn't that Product echo $foundProduct->render(array('mobile' => $foundUser->id)); // render the user page telling the product template which user to use } else { echo $foundUser->render(); // render the user page } } else { // code for the homepage } I used render, but you can write the code directly of course. On the second render, I'm passing the user as an option so you can use it on the products template. Ryan explains options in render here https://processwire.com/talk/topic/3145-multiple-views-for-templates/?p=32876 5 Link to comment Share on other sites More sharing options...
heldercervantes Posted July 29, 2015 Author Share Posted July 29, 2015 I'm amazed with how simple and awesome segments are. As with everything else in PW, the solution is cleverly simple. BUT I've detected a somewhat weird little issue. With that option set to 1 in config.php, I get a 404 when I try to access error logs on the cms. No biggie though, I set it to 2 max segments and the logs are back. 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