Permalink
Browse files

enable jsontemplate to iterate over dicts

  • Loading branch information...
jeromebaum committed Aug 18, 2012
1 parent d8f508f commit 9850d86171d7839e1409bb6ba93dcaf6dc9888b9
Showing with 20 additions and 1 deletion.
  1. +17 −0 dict.patch
  2. +3 −1 jsontemplate.py
View
@@ -0,0 +1,17 @@
+diff --git a/jsontemplate.py b/jsontemplate.py
+index 1b60795..e706799 100644
+--- a/jsontemplate.py
++++ b/jsontemplate.py
+@@ -1094,9 +1094,11 @@ def _DoRepeatedSection(args, context, callback):
+ block = args
+
+ items = context.PushSection(block.section_name)
+- # TODO: if 'items' is a dictionary, allow @name and @value.
+
+ if items:
++ if isinstance(items, dict):
++ items = [{'@name': k, '@value': v} for k,v in items.iteritems()]
++ context.stack[-1].context = items
+ if not isinstance(items, list):
+ raise EvaluationError('Expected a list; got %s' % type(items))
+
View
@@ -1094,9 +1094,11 @@ def _DoRepeatedSection(args, context, callback):
block = args
items = context.PushSection(block.section_name)
- # TODO: if 'items' is a dictionary, allow @name and @value.
if items:
+ if isinstance(items, dict):
+ items = [{'@name': k, '@value': v} for k,v in items.iteritems()]
+ context.stack[-1].context = items
if not isinstance(items, list):
raise EvaluationError('Expected a list; got %s' % type(items))

0 comments on commit 9850d86

Please sign in to comment.