public
Created

Django templatetags example

  • Download Gist
_latest_posts.html
Liquid
1 2 3 4 5
ul>
{% for p in posts %}
<li><a href="{{ a.url }}">{{ a.title }}</a>
{% endfor %}
</ul>
app_tags.py
Python
1 2 3 4 5 6 7 8 9 10 11 12
# This should go in app/templatetags/app_tags.py
 
from django.template import Library
 
from app.models import Post
 
register = Library()
 
@register.inclusion_tag('app/_latest_posts.html')
def latest_posts():
posts = Post.objects.order_by('-date')[:6]
return locals()
base.html
Liquid
1 2 3 4 5 6 7
{% load app_tags %}
 
// Lots of HTML
 
<div>
{% latest_posts %}
</div>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.