from lxml import objectify
from django import template
from django.template.loader import render_to_string
register = template.Library()
class XMLNode(template.Node):
def __init__(self, xml):
self.xml = xml
def render(self, context):
output = self.xml.render(context)
xml = objectify.fromstring(output)
context['object'] = xml
return render_to_string('xml/%s.html' % xml.tag, context)
@register.tag('xml')
def do_xml(parser, token):
"""
Convert template XML to an object which renders a corresponding
HTML template. Lets you embed components using pseudo markup.
In the main template you'll have something like:
{% xml %}