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.