Render category tree on category pages
This commit is contained in:
@@ -31,7 +31,7 @@ final class CategoryController extends BaseController
|
|||||||
$categoriesTree = $this->addToTree($product->categories, $categoriesTree);
|
$categoriesTree = $this->addToTree($product->categories, $categoriesTree);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category, 'categoryTree' => $categoriesTree]);
|
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category, 'category' => $category, 'categoryTree' => $categoriesTree]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addToTree(array $categories, mixed $categoriesTree)
|
private function addToTree(array $categories, mixed $categoriesTree)
|
||||||
|
|||||||
@@ -71,19 +71,20 @@ class AppExtension extends AbstractExtension
|
|||||||
return $items->first(fn($item) => str_starts_with($item->created_at, $date));
|
return $items->first(fn($item) => str_starts_with($item->created_at, $date));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderCategoryTree($categories, $level = 0): string
|
public function renderCategoryTree($categories, $current, $level = 0): string
|
||||||
{
|
{
|
||||||
$tree = '';
|
$tree = '';
|
||||||
if ($level == 0) {
|
if ($level == 0) {
|
||||||
$tree .= '<ul class="list-group">';
|
$tree .= '<ul class="list-group list-group-flush">';
|
||||||
}
|
}
|
||||||
foreach ($categories as $categoryName => $category) {
|
foreach ($categories as $categoryName => $category) {
|
||||||
$tree .= '<a class="list-group-item list-group-item-action text-decoration-none ms-' . ($level * 2) . '" href="' . $this->route->generate('app_category', ['category' => $categoryName]) . '">' . $categoryName . ' <span class="badge bg-primary rounded-pill">' . $category['count'] . '</span></a>';
|
$currentClass = $categoryName == $current ? 'list-group-item-primary' : '';
|
||||||
|
$tree .= '<a class="list-group-item list-group-item-action '.$currentClass.' text-decoration-none ms-' . ($level * 2) . '" href="' . $this->route->generate('app_category', ['category' => $categoryName]) . '">' . $categoryName . ' <span class="badge bg-primary rounded-pill">' . $category['count'] . '</span></a>';
|
||||||
|
|
||||||
unset($category['count']);
|
unset($category['count']);
|
||||||
if (is_array($category) && count($category) >= 1) {
|
if (is_array($category) && count($category) >= 1) {
|
||||||
foreach ($category as $subcategoryName => $subCategory) {
|
foreach ($category as $subcategoryName => $subCategory) {
|
||||||
$tree .= $this->renderCategoryTree([$subcategoryName => $subCategory], $level + 1);
|
$tree .= $this->renderCategoryTree([$subcategoryName => $subCategory], $current, $level + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
{% extends "template.html.twig" %}
|
{% extends "template.html.twig" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
{#{% cache 'list_' ~ listType %}#}
|
{% cache 'list_' ~ listType %}
|
||||||
|
|
||||||
{% if listType starts with 'category_' %}
|
{% if listType starts with 'category_' %}
|
||||||
{{ renderCategoryTree(categoryTree) | raw }}
|
{{ renderCategoryTree(categoryTree, category) | raw }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<div class="table-responsive">
|
<div class="table-responsive">
|
||||||
@@ -71,5 +71,5 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
{#{% endcache %}#}
|
{% endcache %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en" data-bs-theme="light">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||||
|
|||||||
Reference in New Issue
Block a user