Skip to content

Commit

Permalink
Implement main menu (#29)
Browse files Browse the repository at this point in the history
Extract top navigation menu structure from wagtail Page tree.
Extract link for root page from HomePage object.
Detect current page in main menu and set style as `active`.
Render only children that have "Show in menu" flag set.
Show translated menu items by using `caption` if available.
  • Loading branch information
abitrolly committed Aug 25, 2016
1 parent f46063f commit 473b27f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
11 changes: 9 additions & 2 deletions cpm_generic/templates/cpm_generic/tags/mainmenu.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
{% endfor %}
</ul>
<ul class="nav pull-right">
<li class="active">
<a href="/">Home</a>
{% for page, is_active in menupages %}
<li{% if is_active %} class="active"{% endif %}>
{% if page.caption %}
<a href="{{ page.url }}">{{ page.caption }}</a>
{% else %}
<a href="{{ page.url }}">{{ page.title }}</a>
{% endif %}
</li>
{% endfor %}

<li class="divider-vertical"></li>
</ul>
30 changes: 21 additions & 9 deletions cpm_generic/templatetags/cpm_generic_tags.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import re

from django import template
from django.utils import translation
from django.conf import settings

from home.models import HomePage


register = template.Library()


@register.inclusion_tag('cpm_generic/tags/mainmenu.html')
def mainmenu(request):
def get_menu_pages(full_path):
homepage = HomePage.objects.get(slug='home')
children = list(homepage.get_children().live().in_menu().specific())
return [(page, page.url == full_path) for page in [homepage] + children]


def get_language_paths(full_path):
curr_lang = translation.get_language().split('-')[0]
full_path = request.get_full_path()
match = re.match(r'^/%s(?P<path>/.*)$' % curr_lang, full_path)
path = match.group('path') if match else full_path
prefix = '/%s/' % curr_lang
path = full_path
if path.startswith(prefix):
path = path[len(prefix):]

languages = [
return [
(
code,
name,
'/%s%s' % (code, path),
'/%s/%s' % (code, path),
code == curr_lang
) for code, name in settings.LANGUAGES
]

return {'languages': languages}

@register.inclusion_tag('cpm_generic/tags/mainmenu.html')
def mainmenu(request):
full_path = request.get_full_path()
return {'languages': get_language_paths(full_path),
'menupages': get_menu_pages(full_path)}

0 comments on commit 473b27f

Please sign in to comment.