Skip to content

{% nava %}

Concept

Uses django.utils.format_html() to output an <a> element. When the request object is included in the template tag, it will call curr() to vet whether the element should include aria-current=page. This makes it fit for desktop/mobile navbar link.

Inclusion in nav via Django Template Language
1
2
3
4
<nav>
  {% nava 'home' 'Home' css="some css classes here" request=request %}
  {% nava 'about' 'About' css="some css classes here" request=request %}
</nav>
Output HTML after the Template is populated with the Context.
1
2
3
4
<nav><!-- Assume user is presently in the about page -->
  <a href="/home" class="some css classes here">Home</a>
  <a aria-current="page" href="/about" class="some css classes here">About</a>
</nav>

HTML fragment: <a> tag for desktop/mobile navbar links.

Checks if link represented by reverse name url reversible is the current path via the request, if it is provided.

In the latter case, on match of the request.path to the reversible, add aria-current=true

Parameters:

Name Type Description Default
reversible str

The value will be passed to the django.urls.reverse() function without keyword arguments

required
text str | None

The text to incude within the anchor element, if any. Defaults to None.

None
css str | None

If provided, will populate the class attribute of the anchor element. Defaults to None.

None
request HttpRequest | None

The django http request object to ascertain the present path of the request. Defaults to None.

None

Returns:

Name Type Description
SafeText SafeText

The output anchor tag

Source code in django_fragments/templatetags/fragments.py
Python
@register.simple_tag
def nava(
    reversible: str,
    text: str | None = None,
    css: str | None = None,
    request: HttpRequest | None = None,
) -> SafeText:
    """HTML fragment: `<a>` tag for desktop/mobile navbar links.

    Checks if link represented by reverse name url `reversible` is the _current_ path via the request, if it is provided.

    In the latter case, on match of the `request.path` to the `reversible`, add `aria-current=true`

    Args:
        reversible (str): The value will be passed to the `django.urls.reverse()` function without keyword arguments
        text (str | None, optional): The text to incude within the anchor element, if any. Defaults to None.
        css (str | None, optional): If provided, will populate the `class` attribute of the anchor element. Defaults to None.
        request (HttpRequest | None, optional): The django http request object to ascertain the present path of the request. Defaults to None.

    Returns:
        SafeText: The output anchor tag
    """  # noqa: E501
    return format_html(
        "<a {aria} href='{url}' class='{css}'>{text}</a>",
        text=text or "",
        url=reverse(reversible),
        css=css,
        aria=curr(request.path, reversible) if request else "",
    )

curr

Returns a string aria-current for use as an attribute when lhs path matches the reversible value that will be passed to the django.urls.reverse().

Parameters:

Name Type Description Default
lhs str

lhs stands for lefthand side, should be first positional element in the tag

required
reversible str

The value will be passed to the django.urls.reverse() without args, kwargs

required

Returns:

Name Type Description
SafeText SafeText

The text "aria-current=page" if a match occurs, otherwise ""

Source code in django_fragments/templatetags/fragments.py
Python
@register.simple_tag
def curr(lhs: str, reversible: str) -> SafeText:
    """Returns a string `aria-current` for use as an attribute when `lhs` path matches the
    `reversible` value that will be passed to the `django.urls.reverse()`.

    Args:
        lhs (str): lhs stands for lefthand side, should be first positional element in the tag
        reversible (str): The value will be passed to the `django.urls.reverse()` without args, kwargs

    Returns:
        SafeText: The text "aria-current=page" if a match occurs, otherwise ""
    """  # noqa: E501
    return mark_safe("aria-current=page" if lhs == reverse(reversible) else "")