A playground for Django Template tags and filters

The Problem

Any sufficiently large Django project starts to have a wide variety of Template Tags and Filters. Even Django ships with a dizzying array of them that allow you to do all sorts of fun and interesting things. Ellington, our CMS at work, has a ton, and I've been thinking about ways to make tags and filters a bit more accessible to people who are using the CMS.

I'm thinking along the lines of people who are tech savvy, but who were just hit with a huge wall of tags and filters to look at. I want them to be able to really easily play with the functionality and see what it does.

The Solution

I created a proof of concept playground for tags and filters in the django admin. It is released as a simple third party app that I have up on Github. Here is a small 1:40 minute screencast that explains what I did:

Django Admin Playground from Eric Holscher on Vimeo.

It gives you a "Play" link next to each of the tags and filters in the admin. Once you click on that, if the docstring for the tag has a code example, it attempts to parse that out. This allows you to easily test out the examples that you should have in the docstrings for your tags.

It displays the docstring above the input areas and allows you to input context variables (naively) and render the template. It uses Jquery to do an ajax post and response that is displayed on the right side of what the output of the template would be.

A simple example with the Add Template Tag:

Add Template Tag

Caveats

This is very hacky and basic code. Totally just a proof of concept and might not work for you. I think that the ideas are worthwhile, and something that could be included in Django at some point.

Currently the context values are just being parsed at the : and split into a dictionary. If anyone knows a good way to turn a basic list like this (using YAML?) into Django objects, then I would be all ears. I thought about it a little bit but couldn't think of an elegant solution.

Also the parsing of the templatetag syntax out of the templates is incredibly simplistic. If I took some time and played around with ReST I'm sure I could figure out a better way to do that (Pulling out the "code blocks" somehow?). But a basic regex worked well enough to get the idea done.

Feedback welcome.




Comments

1 Greg says...

Perfect to teach django !

Posted at 8:13 p.m. on May 24, 2009

2 Alex Gaynor says...

I'm not sure I understand the point, isn't this what the command line is for? Trying to program in the browser is, in my opinion, idiotic. If you don't know what your template tags and filters do, you have a documentation problem, not a tools problem.

Posted at 8:28 p.m. on May 24, 2009

3 Eric Holscher says...

Greg: Indeed.

Alex: It's not for you :) I agree that programmers and the people writing the code won't really use this. But for people who aren't super technical but are tasked with writing templates, it's a really low barrier to entry way to test out template tags. If they are trying to accomplish a certain goal, they can see if it works in a couple seconds, instead of having the to set up an environment and everything in order to test.

Posted at 8:36 p.m. on May 24, 2009

4 Thejaswi Puthraya says...

Great idea!!! The template system was written with designers in mind and the browser is their playground. So I think this would be very helpful for them.

Posted at 7:40 a.m. on May 26, 2009

Comments support markdown

Comments are closed.

Comments have been close for this post.