Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Get menus list directly from context
Anna Siwikde52414ecc99
diff --git a/docs/architecture/navigation.rst b/docs/architecture/navigation.rstindex fddc294747..48c9721f64 100644--- a/docs/architecture/navigation.rst+++ b/docs/architecture/navigation.rst@@ -9,11 +9,11 @@ You can add up to 3 levels of menu items inside every menu you create. Each menuRendering----------Menu is rendered in templates by passing to a ``menu`` templatetag a list of available menus (attached with context processor) and menu slug, as shown below.+Menu is rendered in templates by passing to a ``menu`` templatetag a menu slug, as shown below... code-block:: html- {% menu menus 'footer' %}+ {% menu 'footer' %}Menu is rendered as a vertical list by default. You can change it by passing an extra ``horizontal=True`` argument.diff --git a/saleor/core/context_processors.py b/saleor/core/context_processors.pyindex 937aca069a..d93b073391 100644--- a/saleor/core/context_processors.py+++ b/saleor/core/context_processors.py@@ -22,7 +22,7 @@ def default_currency(request):def navigation(request):menus = Menu.objects.prefetch_related('items', 'items__collection', 'items__category', 'items__page').all()- return {'menus': list(menus)}+ return {settings.NAVIGATION_CONTEXT_NAME: list(menus)}def search_enabled(request):diff --git a/saleor/core/templatetags/shop.py b/saleor/core/templatetags/shop.pyindex 8200d892e6..c8b4ad056f 100644--- a/saleor/core/templatetags/shop.py+++ b/saleor/core/templatetags/shop.py@@ -1,8 +1,7 @@+from django.conf import settingsfrom django.template import Libraryfrom django.utils.http import urlencode-from ...menu.models import Menu-register = Library()@@ -17,8 +16,9 @@ def get_sort_by_url(context, field, descending=False):return '%s?%s' % (request.path, urlencode(request_get))[email protected]_tag('menu.html')-def menu(menus, slug, horizontal=False):[email protected]_tag('menu.html', takes_context=True)+def menu(context, slug, horizontal=False):+ menus = context[settings.NAVIGATION_CONTEXT_NAME]menu = next((menu for menu in menus if menu.slug == slug), None)menu_items = menu.get_direct_items() if menu else Nonereturn {'menu_items': menu_items, 'horizontal': horizontal}diff --git a/saleor/settings.py b/saleor/settings.pyindex ef602614a8..d517d0367f 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -420,3 +420,7 @@ ALLOWED_ATTRIBUTES = {'a': ['href', 'title'],'img': ['src']}ALLOWED_STYLES = ['text-align']+++# Navigation configuration+NAVIGATION_CONTEXT_NAME = 'menus'diff --git a/templates/base.html b/templates/base.htmlindex 280f762924..f9bb96cd17 100644--- a/templates/base.html+++ b/templates/base.html@@ -149,7 +149,7 @@</div><div class="navbar__menu container d-block"><nav class="navigation">- {% menu menus 'navbar' horizontal=True %}+ {% menu 'navbar' horizontal=True %}</nav></div></header>@@ -178,7 +178,7 @@</a></div><div class="col-md-3 col-sm-6">- {% menu menus 'footer' %}+ {% menu 'footer' %}</div><div class="col-md-3 col-sm-6"><ul>