Piqsel Posted June 29, 2017 Share Posted June 29, 2017 Hi, Sorry in advance for a long post and the lack of knowledge :), but I was wondering how I would go about to modify the scyscraper profile to be used for several different "objects". Right now I have the following tree structure and templates: - Cars (basic-page.php) - New cars (cars.php) - BMW M3 (car.php) - BMW M5 - BMW M7 - Used cars - Volvo S60 - Volvo S90 - Volvo XC90 - Motorcycles (basic-page.php) - New motorcycles (motorcycles.php) - List of new motorcycles (motorcycle.php) - Used motorcycles - List of used motorcycles To render new cars I have the following functions (based off of the Scyscraper profile): function findCars($selector) { $validSorts = getValidSorts(); $sort = sanitizer('name', input()->get('sort')); if(!$sort || !isset($validSorts[$sort])) $sort = 'name'; if($sort != 'name') input()->whitelist('sort', $sort); $selector = "template=car, limit=24, " . trim($selector, ", "); if(strpos($selector, "~=") === false) $selector .= ", sort=$sort"; $cars = pages($selector); return $cars; } function renderCarList(PageArray $cars, $showPagination = true) { $pagination = ''; $sortSelect = ''; $items = array(); if($showPagination && $cars->count()) { $pagination = renderPagination($cars); $sortSelect = renderCarListSort(); } foreach($cars as $car) { $items[] = renderCarListItem($car); } $selector = (string) $cars->getSelectors(); if($selector) $selector = makePrettySelector($selector); $out = files()->render('./includes/car-list.php', array( 'cars' => $cars, 'items' => $items, 'pagination' => $pagination, 'sortSelect' => $sortSelect, 'selector' => $selector )); return $out; } function renderCarListItem(Page $car) { $images = $car->get('images'); if(count($images)) { $thumb = $images->first()->width(200); $img = $thumb->url; } $car->set('unknown', '--'); $out = files()->render('./includes/car-list-item.php', array( 'car' => $car, 'url' => $car->url, 'img' => $img, 'title' => $car->title, 'price' => number_format($car->price, 0, ' ', ' '), 'price_monthly' => number_format($car->price_monthly, 0, ' ', ' '), 'year' => $car->get('year|unknown')->title, 'milage' => $car->get('milage|unknown'), 'fueling' => $car->get('fueling|unknown')->title, 'gearbox' => $car->get('gearbox|unknown')->title )); return $out; } To render the cars I have the following on cars.php: region('main', files()->render('./includes/hero.php') . renderCarsList(findCars('')) ); This works fine, but it seems very redundant to make new functions for every type of vehicle when the listing pages of the vehicles looks almost exactly the same. What I would like to do is change the above functions to be something like findVehicles, renderVehicleList and renderVehicleListItem. I was wondering if this is possible to accomplish by simply changing the $selector from using template=car to instead somehow get the current page childrens template. So on the cars page the selector template becomes car, on motorcycles page the selector becomes motorcycle etc. I tried fiddling around with $page->child->template but can't get it to work. Maybe I should change strategy altogether? Any advice or suggestions will be much appreciated. Link to comment Share on other sites More sharing options...
fbg13 Posted June 30, 2017 Share Posted June 30, 2017 1 hour ago, Piqsel said: get the current page childrens template Try // template name of $page's first child $page->children->first()->template->name; 1 Link to comment Share on other sites More sharing options...
Piqsel Posted June 30, 2017 Author Share Posted June 30, 2017 19 hours ago, fbg13 said: Try // template name of $page's first child $page->children->first()->template->name; Thanks for helping me! I tried changing the follwoing line: $selector = "template=car, limit=24, " . trim($selector, ", "); to $selector = $page->children->first()->template->name . trim($selector, ", "); but that returns error "Undefined variable" for that specific line. When I echo the "$page->children->first()->template->name" I get the correct template though so I guess I'm missing something. Link to comment Share on other sites More sharing options...
fbg13 Posted June 30, 2017 Share Posted June 30, 2017 @Piqsel you're missing "template=" in your selector. Link to comment Share on other sites More sharing options...
Piqsel Posted June 30, 2017 Author Share Posted June 30, 2017 15 minutes ago, fbg13 said: @Piqsel you're missing "template=" in your selector. Hmmmm, tried changing it to the following $selector = "template={$page->children->first()->template->name}, limit=24, " . trim($selector, ", "); but now I get the error "Undefined variable: page" on the same line. Any ideas? Thanks for your patience :). Link to comment Share on other sites More sharing options...
fbg13 Posted June 30, 2017 Share Posted June 30, 2017 Use wire("page") instead of $page in functions. $tpl_name = wire("page")->children->first()->template->name $selector = "template={$tpl_name}"; 1 Link to comment Share on other sites More sharing options...
Piqsel Posted June 30, 2017 Author Share Posted June 30, 2017 4 minutes ago, fbg13 said: Use wire("page") instead of $page in functions. $tpl_name = wire("page")->children->first()->template->name $selector = "template={$tpl_name}"; That did the trick! Forgot about having to use wire('pages') in functions. Thank you so much for your time. 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