I have some partial templates which I load in on various pages and sometimes the inclusion of these partials is dynamic, these have their own related models.
At present I am passing models via views to the main page, but is there not a way to load data for these partials independant of the parent page and view?
Just seems like I am duplicating code in the views which cant be right!
Can I not create a custom tag or something which would allow me to load data into the partial irrespective of the data passed in the parent page and its view?
A good example of this is a partial for "latest posts" which exists in a sidebar partial and loads on many different parent templates
Cheers Kevin
A custom template tag can do this for you. You could write an inclusion tag, which will output the rendered template directly:
# yourapp/templatetags/appname_tags.py
def latest_posts(num_posts):
posts = Post.objects.all()[:num_posts]
return {'posts': posts}
register.inclusion_tag('yourapp/partials/latest_posts.html')(latest_posts)