I've written a templatetag that includes much of the logic that I would normally expect to find in a view. While writing some unit tests I started wondering about a slightly different approach. I don't feel experienced enough to judge the pros and cons (maybe this is even a well known practice – or a no go...). That's why I am interested in your opinion.
My idea was to write a view to handle all the logic and to use the templatetag as a wrapper that passes all relevant context to that view and returns the rendered HTML.
Advantages that I would hope to get from this approach:
For example, a templatetag that renders a tree navigation could deliver HTML when accessed through the templatetag while at the same time its corresponding view remains accessible through a URL.
The view could provide different output formats like JSON, RSS, XML, handle permission checking, ... Advanced logic could be tested through the view, leaving responsibility for the templatetag testcases just to ensure the very basics.
I'd appreciate other opinions, hints or links to packages or related posts.
In my opinion, the problem with template tags are:
What I suggest instead is:
The benefits you get by this approach are: