How to parse Django templates for template tags

django, python

Solution

You can also use the `compile_string` method.

 >>> from django.template.base import *
 >>> settings.configure()
 >>> compile_string("<a href='ab'></a>{% cycle 'row1' 'row2' as rowcolors %}", None)
 >>> [<Text Node: '<a href='ab'></a>'>, <django.template.defaulttags.CycleNode object at 0x10511b210>]

The compile string method is utilized by the `Template` class and is the method used to produce the node list. Tested in Django 1.8 Alpha.

https://github.com/django/django/blob/1f8bb95cc2286a882e0f7a4692f77b285d811d11/django/template/base.py

Problem

Situation I'm writing a checker program that checks Django templates. For example I want to check if all Django templates that use `url` template tag, use it with quotes on first parameter so that it is Django 1.5 compatible. Also I want to check that they have included `{% load url from future %}` in their templates. For example if my program parses the following Django template, I want it to raise an exception. ``` {% extends 'base.html' %} <td> <a href="{% url first second %}"> </a> </td> ``` But this template should get parsed without exception. ``` {% extends 'base.html' %} {% load url from future %} <td> <a href="{% url 'first' second %}"> </a> </td> ``` I'm not limited to this simple example. I have other parsings to do. For example I want to check how many `load` template tags are present in the template. Question How can I elegantly solve this parsing problem? - I don't want to use regular expressions. - I this Django it self has some utilities in this regard. I think using them is a good idea, but I don't know how. - I want to run the program separately from Django. So I don't want Django to run the program itself (with `render_to_response`). (This is important) Code Please show me some code that can solve the example I mentioned. I want to detect whether `{% load url from future %}` is in the code. Also I want to check every `url` template tag and check if the first argument is quoted. Bonus: - I want to be able to see the rendered HTML that Django generates from this template, and do my HTML parsing on it. (for example with PyQuery)

Original source