1👍
An inclusion tag will most likely do what you’re looking for.
Inclusion tags can accept the current template context and you can retrieve your view values from there instead of having to manually pass them in. By including a template that contains the div, anchor tag, etc, you keep your templates simple:
{% tag_url %}
instead of having to repeat a bunch of conditional statements and loops.
I wouldn’t recommend doing this sort of logic in a view, as it’s not encapsulated or re-usable.
Source:stackexchange.com