Skip to content

Commit 772a955

Browse files
committed
add option to hide empty menu items, refs #23
1 parent 6862c39 commit 772a955

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

docs/configuration.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ MENU_SELECT_PARENTS
1111
their ``selected`` property set to ``True`` if one of their children has its
1212
``selected`` property set to ``True``.
1313

14+
MENU_HIDE_EMPTY
15+
-------------------
16+
**Default: ``False``**
17+
18+
``MENU_HIDE_EMPTY`` controls if menu items without an explicit ``check`` callback
19+
should be visible even if they have no children
20+
1421

1522
.. _Django settings file: https://docs.djangoproject.com/en/dev/topics/settings/

menu/menu.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,25 @@ def process(self, request):
185185
if not self.visible:
186186
return
187187

188+
# evaluate title
188189
self.check_title(request)
189-
self.check_children(request)
190190

191-
children = [
192-
kid
193-
for kid in self.children
194-
if kid.visible
195-
]
191+
# evaluate children
192+
visible_children = []
193+
self.check_children(request)
194+
for child in self.children:
195+
child.process(request)
196+
if child.visible:
197+
visible_children.append(child)
198+
199+
if getattr(
200+
settings, 'MENU_HIDE_EMPTY', False
201+
) and not self.check and not len(visible_children):
202+
self.visible = False
203+
return
196204

197205
curitem = None
198-
for item in children:
199-
item.process(request)
206+
for item in visible_children:
200207
item.selected = False
201208

202209
if item.match_url(request):

0 commit comments

Comments
 (0)