is_array($_GET['tags'] ?? '')
? $_GET['tags']
: array_filter(array_map('trim', explode(',', $_GET['tags'] ?? ''))),
default => array_filter(array_map('trim', explode(',', '')))
};
if (!empty($tags)) {
$filters['tags'] = implode(',', $tags);
}
}
// Author filtering
$filterAuthor = "all";
if ($filterAuthor !== 'all') {
$author = match ($filterAuthor) {
'url' => is_array($_GET['author'] ?? '')
? $_GET['author']
: array_filter(array_map('trim', explode(',', $_GET['author'] ?? ''))),
default => array_filter(array_map('trim', explode(',', '')))
};
if (!empty($author)) {
$filters['author'] = implode(',', $author);
}
}
// Featured filtering
$filterFeatured = "all";
if ($filterFeatured !== 'all') {
$filters['featured'] = $filterFeatured === 'true' ? 'true' : 'false';
}
// Status filtering
$filterStatus = "published";
if ($filterStatus !== 'all') {
$filters['status'] = $filterStatus;
} else {
$filters['status'] = 'published'; // Default to published only
}
// Date filtering - handled via API query parameters
$filterDate = "past";
if ($filterDate === 'past') {
$filters['date_before'] = date('Y-m-d H:i:s');
} elseif ($filterDate === 'future') {
$filters['date_after'] = date('Y-m-d H:i:s');
}
$orderBy = "date";
$orderDirection = "desc";
// Build collection query using fluent API with URL configuration
$ECMS_options = [
'resources' => [
'path' => '../../resources'
]
];
$query = cms($ECMS_options)->collection($collectionPath)
->pagePath($pagePath)
->tagPagePath('')
->authorPagePath('')
->prettyUrls($prettyUrls)
->orderBy($orderBy, $orderDirection);
// Apply all filters
foreach ($filters as $key => $value) {
switch ($key) {
case 'tags':
$query = $query->tags($value);
break;
case 'author':
$query = $query->author($value);
break;
case 'featured':
$query = $query->featured($value === 'true');
break;
case 'status':
$query = $query->status($value);
break;
case 'date_before':
$query = $query->before($value);
break;
case 'date_after':
$query = $query->after($value);
break;
}
}
// Execute query with pagination
try {
$collection = $query
->paginate($currentPage, $itemsPerPage)
->get();
$items = $collection->items;
$pagination = $collection->pagination;
} catch (Exception $e) {
$items = [];
$pagination = (object) [
'current_page' => 1,
'total_pages' => 1,
'total_items' => 0,
'items_per_page' => $itemsPerPage,
'has_prev' => false,
'has_next' => false
];
}
// Create collection-like object for backward compatibility
$collectionObj = new class($items, $pagination) {
public $items;
public $pagination;
public function __construct($items, $pagination)
{
$this->items = $items;
$this->pagination = $pagination;
}
public function each($callback)
{
foreach ($this->items as $item) {
$callback($item);
}
}
};
// Collection data is now available in template scope
$collection = $collectionObj;
?> $collection->pagination,
'items' => $collection->items,
'collection' => $collection,
'filters' => $filters,
'orderBy' => $orderBy,
'orderDirection' => $orderDirection,
'collectionPath' => $collectionPath,
'pagePath' => $pagePath,
'itemsPerPage' => $itemsPerPage,
'prettyUrls' => $prettyUrls,
'collectionItemTemplateId' => $collectionItemTemplateId ?? null
]);
?>
each(function ($item) use ($collection, $collectionItemTemplate, $filters, $orderBy, $orderDirection, $collectionPath, $pagePath, $itemsPerPage, $prettyUrls, $collectionItemTemplateId) {
echo renderTemplate($collectionItemTemplate, [
'item' => $item,
'collection' => $collection,
'filters' => $filters,
'orderBy' => $orderBy,
'orderDirection' => $orderDirection,
'collectionPath' => $collectionPath,
'pagePath' => $pagePath,
'itemsPerPage' => $itemsPerPage,
'prettyUrls' => $prettyUrls,
'collectionItemTemplateId' => $collectionItemTemplateId
]);
});
// Output the template for use in frontend Alpine components
echo '';
echo $collectionItemTemplate;
echo '';
?>
$collection->pagination,
'items' => $collection->items,
'collection' => $collection,
'filters' => $filters,
'orderBy' => $orderBy,
'orderDirection' => $orderDirection,
'collectionPath' => $collectionPath,
'pagePath' => $pagePath,
'itemsPerPage' => $itemsPerPage,
'prettyUrls' => $prettyUrls,
'collectionItemTemplateId' => $collectionItemTemplateId
]);
?>
wrapper around {{item.body}} since body already contains HTML
$itemTemplate = str_replace('
', '{{item.body}}', $itemTemplate);
if ($item) {
echo renderTemplate($itemTemplate, ['item' => $item]);
} else {
// @TODO: Add a "Not Found" template (or redirect option)
echo 'No item found';
}
?>