diff options
author | Jonathan DeMasi <jrdemasi@gmail.com> | 2020-03-08 15:12:01 -0600 |
---|---|---|
committer | Jonathan DeMasi <jrdemasi@gmail.com> | 2020-03-08 15:12:01 -0600 |
commit | 12af89cb5d418cc63587a454ed1d461da26fd36a (patch) | |
tree | 618a232f0b6587791368ca67932f1ac15c9c835e | |
parent | 74ff789b5c6625804124971b5045f7cae49ff238 (diff) | |
download | jthanio-12af89cb5d418cc63587a454ed1d461da26fd36a.tar jthanio-12af89cb5d418cc63587a454ed1d461da26fd36a.tar.gz jthanio-12af89cb5d418cc63587a454ed1d461da26fd36a.tar.bz2 jthanio-12af89cb5d418cc63587a454ed1d461da26fd36a.tar.lz jthanio-12af89cb5d418cc63587a454ed1d461da26fd36a.tar.xz jthanio-12af89cb5d418cc63587a454ed1d461da26fd36a.tar.zst jthanio-12af89cb5d418cc63587a454ed1d461da26fd36a.zip |
navigation working
-rw-r--r-- | home/templates/home/tags/top_menu.html | 24 | ||||
-rw-r--r-- | home/templatetags/navigation_tags.py | 73 | ||||
-rw-r--r-- | jthanio/templates/base.html | 1 | ||||
-rw-r--r-- | jthanio/templates/navigation.html | 30 | ||||
-rw-r--r-- | jthanio/templatetags/custom_tags.py | 0 |
5 files changed, 102 insertions, 26 deletions
diff --git a/home/templates/home/tags/top_menu.html b/home/templates/home/tags/top_menu.html new file mode 100644 index 0000000..3a228ed --- /dev/null +++ b/home/templates/home/tags/top_menu.html @@ -0,0 +1,24 @@ +{% load navigation_tags wagtailcore_tags %} +{% get_site_root as site_root %} + +<nav class="navbar navbar-expand-lg navbar-dark bg-dark"> + <div class="container"> + <a class="navbar-brand" href="/">jthan.io</a> + <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation"> + <span class="navbar-toggler-icon"></span> + </button> + + <div class="collapse navbar-collapse" id="navbarColor02"> + <ul class="navbar-nav mr-auto"> + <li class="nav-item {% if request.path == '/' %}active{% endif %}"> + <a class="nav-link" href="/">Home{% if request.path == '/' %} <span class="sr-only">(current)</span>{% endif %}</a> + </li> + {% for menuitem in menuitems %} + <li class="nav-item {% if menuitem.active %}active{% endif %}"> + <a class="nav-link" href="{% pageurl menuitem %}">{{ menuitem.title }}{% if menuitem.active %} <span class="sr-only">(current)</span>{% endif %}</a> + </li> + {% endfor %} + </ul> + </div> + </div> + </nav>
\ No newline at end of file diff --git a/home/templatetags/navigation_tags.py b/home/templatetags/navigation_tags.py new file mode 100644 index 0000000..447be3b --- /dev/null +++ b/home/templatetags/navigation_tags.py @@ -0,0 +1,73 @@ +from django import template + +from wagtail.core.models import Page + +register = template.Library() +# https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/ + + +@register.simple_tag(takes_context=True) +def get_site_root(context): + # This returns a core.Page. The main menu needs to have the site.root_page + # defined else will return an object attribute error ('str' object has no + # attribute 'get_children') + return context['request'].site.root_page + + +def has_menu_children(page): + # This is used by the top_menu property + # get_children is a Treebeard API thing + # https://tabo.pe/projects/django-treebeard/docs/4.0.1/api.html + return page.get_children().live().in_menu().exists() + + +def has_children(page): + # Generically allow index pages to list their children + return page.get_children().live().exists() + + +def is_active(page, current_page): + # To give us active state on main navigation + return (current_page.url_path.startswith(page.url_path) if current_page else False) + + +# Retrieves the top menu items - the immediate children of the parent page +# The has_menu_children method is necessary because the Foundation menu requires +# a dropdown class to be applied to a parent +@register.inclusion_tag('home/tags/top_menu.html', takes_context=True) +def top_menu(context, parent, calling_page=None): + menuitems = parent.get_children().live().in_menu() + for menuitem in menuitems: + menuitem.show_dropdown = has_menu_children(menuitem) + # We don't directly check if calling_page is None since the template + # engine can pass an empty string to calling_page + # if the variable passed as calling_page does not exist. + menuitem.active = (calling_page.url_path.startswith(menuitem.url_path) + if calling_page else False) + return { + 'calling_page': calling_page, + 'menuitems': menuitems, + # required by the pageurl tag that we want to use within this template + 'request': context['request'], + } + + +# Retrieves the children of the top menu items for the drop downs +@register.inclusion_tag('home/tags/top_menu_children.html', takes_context=True) +def top_menu_children(context, parent, calling_page=None): + menuitems_children = parent.get_children() + menuitems_children = menuitems_children.live().in_menu() + for menuitem in menuitems_children: + menuitem.has_dropdown = has_menu_children(menuitem) + # We don't directly check if calling_page is None since the template + # engine can pass an empty string to calling_page + # if the variable passed as calling_page does not exist. + menuitem.active = (calling_page.url_path.startswith(menuitem.url_path) + if calling_page else False) + menuitem.children = menuitem.get_children().live().in_menu() + return { + 'parent': parent, + 'menuitems_children': menuitems_children, + # required by the pageurl tag that we want to use within this template + 'request': context['request'], + } diff --git a/jthanio/templates/base.html b/jthanio/templates/base.html index f288bf3..694f86c 100644 --- a/jthanio/templates/base.html +++ b/jthanio/templates/base.html @@ -1,5 +1,4 @@ {% load static wagtailuserbar %} - <!DOCTYPE html> <html class="no-js" lang="en"> <head> diff --git a/jthanio/templates/navigation.html b/jthanio/templates/navigation.html index 54756a2..0b6026d 100644 --- a/jthanio/templates/navigation.html +++ b/jthanio/templates/navigation.html @@ -1,25 +1,5 @@ -<nav class="navbar navbar-expand-lg navbar-dark bg-dark"> - <div class="container"> - <a class="navbar-brand" href="#">jthan.io</a> - <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation"> - <span class="navbar-toggler-icon"></span> - </button> - - <div class="collapse navbar-collapse" id="navbarColor02"> - <ul class="navbar-nav mr-auto"> - <li class="nav-item active"> - <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> - </li> - <li class="nav-item"> - <a class="nav-link" href="#">Features</a> - </li> - <li class="nav-item"> - <a class="nav-link" href="#">Pricing</a> - </li> - <li class="nav-item"> - <a class="nav-link" href="#">About</a> - </li> - </ul> - </div> - </div> - </nav>
\ No newline at end of file +{% load navigation_tags %} +{% block menu %} + {% get_site_root as site_root %} + {% top_menu parent=site_root calling_page=self %} +{% endblock %} diff --git a/jthanio/templatetags/custom_tags.py b/jthanio/templatetags/custom_tags.py deleted file mode 100644 index e69de29..0000000 --- a/jthanio/templatetags/custom_tags.py +++ /dev/null |