Created
July 27, 2011 06:55
-
-
Save nathanborror/1108817 to your computer and use it in GitHub Desktop.
Revisions
-
nathanborror revised this gist
Jul 31, 2011 . 1 changed file with 5 additions and 42 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ from lxml import objectify from django import template from django.template.loader import render_to_string @@ -12,8 +12,8 @@ def __init__(self, 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) @@ -35,49 +35,12 @@ def do_xml(parser, token): Which renders the template `xml/tabs.html` where you can: <div class="tabs"> {% for tab in object.tab %} <a href="{{ tab.attrib.url }}">{{ tab.attrib.label }}</a> {% endfor %} </div> """ bits = token.split_contents() xml = parser.parse(('endxml',)) parser.delete_first_token() return XMLNode(xml) -
nathanborror revised this gist
Jul 27, 2011 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,17 +20,19 @@ def render(self, 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 %} <tabs> <tabs url="http://facebook.com" label="Facebook" /> <tabs url="http://apple.com" label="Apple" /> </tabs> {% endxml %} Which renders the template `xml/tabs.html` where you can: <div class="tabs"> {% for tab in object.value %} -
nathanborror revised this gist
Jul 27, 2011 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,11 +7,11 @@ class XMLNode(template.Node): def __init__(self, xml): self.xml = xml def render(self, context): output = self.xml.render(context) xml = etree.fromstring(output) context['object'] = dictlist(xml) return render_to_string('xml/%s.html' % xml.tag, context) @@ -39,9 +39,9 @@ def do_xml(parser, token): </div> """ bits = token.split_contents() xml = parser.parse(('endxml',)) parser.delete_first_token() return XMLNode(xml) # -
nathanborror revised this gist
Jul 27, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,7 @@ def do_xml(parser, token): <div class="tabs"> {% for tab in object.value %} <a href="{{ tab.attributes.url }}">{{ tab.attributes.label }}</a> {% endfor %} </div> """ -
nathanborror created this gist
Jul 27, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ from lxml import etree from django import template from django.template.loader import render_to_string register = template.Library() class XMLNode(template.Node): def __init__(self, nodelist): self.nodelist = nodelist def render(self, context): output = self.nodelist.render(context) xml = etree.fromstring(output) context['object'] = dictlist(xml) return render_to_string('xml/%s.html' % xml.tag, context) @register.tag('xml') def do_xml(parser, token): """ Convert XML within a template into an object to be used in a corresponding HTML template. Lets you embed components using pseudo markup. {% xml %} <tabs> <tabs url="http://facebook.com" label="Facebook" /> <tabs url="http://apple.com" label="Apple" /> </tabs> {% endxml %} Renders the template `xml/tabs.html` where you can: <div class="tabs"> {% for tab in object.value %} <a href="{{ tab.attributes.url }}">{{ tab.attributes.label }}</a> {% endfor %} </div> """ bits = token.split_contents() nodelist = parser.parse(('endxml',)) parser.delete_first_token() return XMLNode(nodelist) # # XML to Dict # def dictlist(node): res = {} res[node.tag] = [] xmltodict(node, res[node.tag]) return { 'value': res[node.tag], 'attributes': node.attrib } def xmltodict(node,res): rep = {} if len(node): for n in list(node): rep[node.tag] = [] value = xmltodict(n, rep[node.tag]) if len(n): value = { 'value': rep[node.tag], 'attributes': n.attrib } res.append(value) else : res.append(rep[node.tag][0]) else: value = { 'value': node.text, 'attributes': node.attrib } res.append(value) return