https://github.com/ehamiter/django-whiff
A simple Django template tag that combines the functionality of the `with` and `if` tags
https://github.com/ehamiter/django-whiff
django python templatetags
Last synced: 5 days ago
JSON representation
A simple Django template tag that combines the functionality of the `with` and `if` tags
- Host: GitHub
- URL: https://github.com/ehamiter/django-whiff
- Owner: ehamiter
- License: mit
- Created: 2024-09-17T23:16:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-18T00:02:55.000Z (over 1 year ago)
- Last Synced: 2025-02-03T04:11:30.269Z (11 months ago)
- Topics: django, python, templatetags
- Language: Python
- Homepage: https://pypi.org/project/django-whiff/
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# django-whiff
`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.
## Example
```django
{% load whiff %}
{% whiff current_user as user %}
Welcome back, {{ user.first_name }} {{ user.last_name }}!
Your last login was on {{ user.last_login|date:"F j, Y, g:i a" }}.
Your Profile
- Email: {{ user.email }}
- Joined: {{ user.date_joined|date:"F j, Y" }}
- Bio: {{ user.bio }}
{% endwhiff %}
```
## Features
- Combines `with` and `if` in a single template tag.
- Simplifies template logic by reducing the need for nested tags.
- Easy to use and integrate into existing Django projects.
## Caveats
- Not a great contender to replace multiple pipes or conditions in an existing template tag
## Installation
You can install `django-whiff` via pip:
```bash
pip install django-whiff
```
## Usage
Once installed, add `django_whiff` to your `INSTALLED_APPS` in your Django settings:
```python
INSTALLED_APPS = [
...
'django_whiff',
...
]
```
> Note *the underscore*, not hyphen.
### How It Works
- The `whiff` tag assigns the value of `some_obj` to `obj`.
- If `some_obj` is truthy, the content inside the `whiff` block is rendered.
- If `some_obj` is falsey, nothing is rendered.
## Tests
```
python -m unittest discover -s tests
```
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
## Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue on the [GitHub repository](https://github.com/ehamiter/django-whiff).
## Acknowledgements
Inspired by the Python walrus operator (`:=`) and the need for cleaner, more concise template logic.