The best solution for this is to write a custom assignment_tag
. This solution is more clean than using a with
tag because it achieves a very clear separation between logic and styling.
Start by creating a template tag file (eg. appname/templatetags/hello_world.py
):
from django import template
register = template.Library()
@register.assignment_tag
def get_addressee():
return "World"
Now you may use the get_addressee
template tag in your templates:
{% load hello_world %}
{% get_addressee as addressee %}
<html>
<body>
<h1>hello {{addressee}}</h1>
</body>
</html>