A simple Django template tag that combines the functionality of the `with` and `if` tags
MIT License
django-whiff
is a simple Django template tag that combines the functionality of the with
and if
tags, inspired by the Python walrus operator (:=
). It allows you to assign a variable and immediately check if it is truthy, rendering content if it is.
{% load whiff %}
{% whiff current_user as user %}
<h1>Welcome back, {{ user.first_name }} {{ user.last_name }}!</h1>
<p>Your last login was on {{ user.last_login|date:"F j, Y, g:i a" }}.</p>
<h2>Your Profile</h2>
<ul>
<li>Email: {{ user.email }}</li>
<li>Joined: {{ user.date_joined|date:"F j, Y" }}</li>
<li>Bio: {{ user.bio }}</li>
</ul>
{% endwhiff %}
with
and if
in a single template tag.You can install django-whiff
via pip:
pip install django-whiff
Once installed, add django_whiff
to your INSTALLED_APPS
in your Django settings:
INSTALLED_APPS = [
...
'django_whiff',
...
]
Note the underscore, not hyphen.
whiff
tag assigns the value of some_obj
to obj
.some_obj
is truthy, the content inside the whiff
block is rendered.some_obj
is falsey, nothing is rendered.python -m unittest discover -s tests
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository.
Inspired by the Python walrus operator (:=
) and the need for cleaner, more concise template logic.