Django template using variable in two blocks -
this template code:
{% block content %} {% get_latest latest_posts %} <ul> {% post in latest_posts %} <li> <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p> </li> {% endfor %} </ul> {% endblock %} {% block sidebar %} <ul> {% post in latest_posts %} <li> <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p> </li> {% endfor %} </ul> {% endblock %}
in content block
for loop
works, in sidebar block
can't use variable latest_posts
. can me this?
the variable's scope limited containing {% block content %}
. can either repeat declaration inside {% block sidebar %}
, or move level it's outside of {% block content %}
.
example
{% get_latest latest_posts %} {% block content %} <ul> {% post in latest_posts %} <li> <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p> </li> {% endfor %} </ul> {% endblock %} {% block sidebar %} <ul> {% post in latest_posts %} <li> <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p> </li> {% endfor %} </ul> {% endblock %}
or
{% block content %} {% get_latest latest_posts %} <ul> {% post in latest_posts %} <li> <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p> </li> {% endfor %} </ul> {% endblock %} {% block sidebar %} {% get_latest latest_posts %} <ul> {% post in latest_posts %} <li> <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p> </li> {% endfor %} </ul> {% endblock %}
Comments
Post a Comment