side_menu_categories¶
The side_menu_categories
macro renders a list of categories in the side menu.
Definition¶
Input parameters¶
categories¶
CategoriesList
Object of CategoriesList type that represents a list of categories to display.
activeCategory¶
Category
Object of Category type representing the currently active category.
options¶
object
represents an object of side menu categories options and attributes:
Option key | Type | Default | Required | Description |
---|---|---|---|---|
options.showAllSubcategories | boolean |
false |
no | If set to true , displays all subcategories |
options.showNewProducts | boolean |
false |
no | If set to true , shows new products in categories |
options.showSpecialOffers | boolean |
false |
no | If set to true , shows special offers in categories |
Example¶
In this example we render a side menu with all subcategories, new products, and special offers displayed if available.
To get the required variables, we use the getCategories() and getCategory() methods from ObjectApi.
The category_id
variable is available in all contexts and is used to determine the currently active category.
{% set categories = ObjectApi.getCategories(true) %}
{% if category_id %}
{% set activeCategory = ObjectApi.getCategory(category_id) %}
{% endif %}
{{ side_menu_categories(categories, activeCategory, {
showAllSubcategories: 1,
showNewProducts: 1,
showSpecialOffers: 1
})
}}
Macro source code¶
{% macro side_menu_categories(categories, activeCategory, options) %}
{% from "@macros/side_menu_category.twig" import side_menu_category %}
{% set shopUrls = ObjectApi.getShopUrls() %}
{% set activeCategoryParent = activeCategory.parentCategory %}
{% set activeCategoryGrandparent = activeCategoryParent ? activeCategoryParent.parentCategory : null %}
{% set isActiveCategoryRoot = activeCategory.isRoot %}
{% set isActiveCategoryParentRoot = activeCategoryParent and activeCategoryParent.isRoot %}
{% set isActiveCategoryGrandparentRoot = activeCategoryGrandparent and activeCategoryGrandparent.isRoot %}
{% set isActiveSubtreeRootRelated = isActiveCategoryGrandparentRoot or isActiveCategoryParentRoot or isActiveCategoryRoot %}
{% set showAllSubcategories = options.showAllSubcategories == 1 %}
{% set shouldShowActiveSubtreeOnly = not isActiveSubtreeRootRelated and not showAllSubcategories %}
{% if activeCategory and shouldShowActiveSubtreeOnly and not showAllSubcategories %}
<a class="link side-menu-categories__list-go-back-link" href="{{ activeCategoryGrandparent.url }}">
{{ translate('Back to: %s', activeCategoryGrandparent.name) }}
</a>
{{
side_menu_category(activeCategoryParent, activeCategory, {
level: 1,
showAllSubcategories,
activeCategoryParent,
activeCategoryGrandparent
})
}}
{% else %}
{% for category in categories %}
{{
side_menu_category(category, activeCategory, {
level: 1,
showAllSubcategories,
activeCategoryParent,
activeCategoryGrandparent
})
}}
{% endfor %}
{% if options.showNewProducts %}
<li class="side-menu-categories__list-item side-menu-categories__list-level-1">
<a href="{{ shopUrls.newProductsUrl }}" class="side-menu-categories__list-item-link link link_secondary link_no-underline">
{{ translate('New products <<side_menu_categories>>') }}
</a>
</li>
{% endif %}
{% if options.showSpecialOffers %}
<li
class="
side-menu-categories__list-item
side-menu-categories__list-item_secondary
side-menu-categories__list-level-1
"
>
<a
href="{{ shopUrls.promotionsUrl }}"
class="side-menu-categories__list-item-link side-menu-categories__list-item-link_secondary link link_no-underline"
>
{{ translate('Special offers <<side_menu_categories>>') }}
</a>
</li>
{% endif %}
{% endif %}
{% endmacro %}