Skip to content
Snippets Groups Projects
Commit d432e6ac authored by Tim Fechner's avatar Tim Fechner
Browse files

Add breadcrumb utilities

To learn more about using breadcrumbs, read the WIKI:
https://lab.it.hs-hannover.de/django/hshassets/wikis/breadcrumbs
parent 94edea22
No related branches found
No related tags found
No related merge requests found
...@@ -33,6 +33,25 @@ The current version of this package ships the following own stuff and awesome th ...@@ -33,6 +33,25 @@ The current version of this package ships the following own stuff and awesome th
Simply put this app into your projects `requirements.txt` file and run `pip install -r requirements.txt`. Once you Simply put this app into your projects `requirements.txt` file and run `pip install -r requirements.txt`. Once you
installed it, you should put the name `hshcdn` into your projects `INSTALLED_APPS` setting. Tadaa! installed it, you should put the name `hshcdn` into your projects `INSTALLED_APPS` setting. Tadaa!
Also if you want to use breadcrubs (you likely do), you need to add the settings context processor to your `TEMPLATES` setting:
```python
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# [...]
'OPTIONS': {
'context_processors': [
# [...]
'hshassets.context_processors.settings' # <-- add this line
],
},
},
]
```
**More information about using breadcrumbs [can be found in the WIKI](https://lab.it.hs-hannover.de/django/hshassets/wikis/breadcrumbs)**
## Usage ## Usage
Simply create an `assets` folder in your app(s) directory. It can contain the folders `fonts` for custom fonts, `img` Simply create an `assets` folder in your app(s) directory. It can contain the folders `fonts` for custom fonts, `img`
......
...@@ -25,3 +25,6 @@ COLOR_SCHEME = 'service' ...@@ -25,3 +25,6 @@ COLOR_SCHEME = 'service'
SCSS_INCLUDE_PATHS = [ SCSS_INCLUDE_PATHS = [
('hshassets', ['sass/lib/']), ('hshassets', ['sass/lib/']),
] ]
# URI to the live service.it portal
SERVICE_IT_URI = 'https://service.it.hs-hannover.de/'
"""
hshassets brings an own middleware to make some settings variables available in templates
"""
from hshassets.app_settings import SERVICE_IT_URI
def settings(request):
return {'service_it_uri': SERVICE_IT_URI}
{% load i18n %}
{% comment %}
This template provides an breadcrumb bar for the hero / hero_keyvisual template. Use it like this:
{% block pre-section %}
{% include "hshassets/includes/breadcrumbs.html" %}
{% endblock %}
This snippet will render a breadcrumb bar under the <div class="hero"> element. To use it, you need to edit your
views that they inherit from "hshassets.views.BreadCrumbMixin". Also provide the attributes "breadcrumb_name" and
"breadcrumb_parent" (only if you have a parent, otherwise the view will be the root element). Example:
class ExploreView(ListView, BreadCrumbMixin):
template_name = 'myapp/explore.html'
breadcrumb_name = _('Explore')
breadcrumb_parent = 'about'
Please note that this template should be used via {% include "" %}, not {% extends "" %}!
This way you can specify your own navigation or hero text or general content stuff.
{% endcomment %}
<div class="breadcrumb-wrapper">
<div class="container">
<nav class="breadcrumb" aria-label="breadcrumbs">
<ul>
<li><a href="{{ service_it_uri }}"><span class="icon"><i class="fa fa-home"></i></span> IT Services</a></li>
{% for breadcrumb in breadcrumb_path %}
<li {% if forloop.last %}class="is-active"{% endif %}>
<a href="{{ breadcrumb.url }}">{{ breadcrumb.name }}</a>
</li>
{% endfor %}
</ul>
</nav>
</div>
</div>
from django.urls import reverse, resolve
from django.views.generic.base import ContextMixin
class BreadCrumbMixin(ContextMixin):
breadcrumb_parent = None
breadcrumb_name = None
def get_breadcrumb_path(self, breadcrumb_path, url_name=''):
if self.breadcrumb_parent:
parent_view_class = resolve(reverse(self.breadcrumb_parent)).func.view_class
breadcrumb_path.extend(parent_view_class().get_breadcrumb_path(breadcrumb_path, self.breadcrumb_parent))
return breadcrumb_path + [{'name': self.breadcrumb_name, 'url': reverse(url_name) if url_name else '#'}]
def get_context_data(self, *args, **kwargs):
assert self.breadcrumb_name, 'Missing attribute "breadcrumb_name" on {}'.format(self.__class__)
context = super().get_context_data(*args, **kwargs)
context.update({
'breadcrumb_path': self.get_breadcrumb_path([])
})
return context
...@@ -7,7 +7,7 @@ os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) ...@@ -7,7 +7,7 @@ os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
setup( setup(
name='django-hshassets', name='django-hshassets',
version='1.2.2', version='1.3.0',
packages=find_packages(), packages=find_packages(),
include_package_data=True, include_package_data=True,
license='MIT License', license='MIT License',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment