Skip to content
Extract template nodes from a Django template
Python HTML
Find file
Failed to load latest commit information.
template_analyzer Bump to v1.6.1 Mar 23, 2016
.gitignore Add wheel support Sep 3, 2014
.travis.yml Travis: add slack notifications Apr 6, 2016
AUTHORS Add packaging files to make it installable. Dec 5, 2011
CHANGES.rst Bump to v1.6.1 Mar 23, 2016
LICENSE
MANIFEST.in Add packaging files to make it installable. Dec 5, 2011
README.rst
runtests.py
setup.cfg Add wheel support Sep 3, 2014
setup.py
tox.ini Add travis settings, update tox Feb 23, 2016

README.rst

django-template-analyzer

https://travis-ci.org/edoburu/django-template-analyzer.svg?branch=master

The template_analyzer package offers an API to analyze the Django template structure. It can be used to find nodes of a particular type, e.g. to do automatic detection of placeholder tags.

Supported features

The scanner finds tags in various situations, including:

  • Extend nodes
  • Include nodes
  • Overwritten blocks with new definitions
  • Blocks with or without {{ block.super }}
  • Reorganized blocks
  • Ignoring nodes outside blocks in extending templates
  • Handling multiple levels of super includes

The returned nodes are provided in a natural ordering, as they would be expected to appear in the outputted page.

While Django offers a template.nodelist.get_nodes_of_type() function, this function does not produce the same results.

API example

from django.template.loader import get_template
from mycms.templatetags.placeholdertags import Placeholder
from template_analyzer.djangoanalyzer import get_node_instances

# Load a Django template
template = get_template("mycms/default-page.html")

# Find all tags in the template:
placeholders = get_node_instances(template, Placeholder)

# Read information from the template tag themselves:
# (this is an example, accessing a custom method on the Placeholder object)
placeholder_names = [p.get_name() for p in placeholders]

Installation

First install the module, preferably in a virtual environment. It can be installed from PyPI:

pip install django-template-analyzer

Or the current folder can be installed:

pip install .

Credits

  • This package is based on the work of Django CMS.
  • Many thanks to the contributors of cms/utils/placeholder.py / cms/utils/plugins.py in Django CMS!
Something went wrong with that request. Please try again.