@@ -810,6 +810,130 @@ def test_tricky_expression(self):
810810 ),
811811 )
812812
813+ def test_dict_expression (self ):
814+ template = """
815+ <%def name="dtest(d)">
816+ % for k,v in d.items():
817+ ${k} = ${v}
818+ % endfor
819+ </%def>
820+
821+ <%self:dtest d="${
822+ {
823+ 'id':'4',
824+ 'foo':'barr'
825+ }
826+ }" />
827+ """
828+ nodes = Lexer (template ).parse ()
829+ self ._compare (
830+ nodes ,
831+ TemplateNode (
832+ {},
833+ [
834+ Text ('\n ' , (1 , 1 )),
835+ DefTag (
836+ 'def' ,
837+ {'name' : 'dtest(d)' }, (2 , 9 ),
838+ [
839+ Text ('\n ' , (2 , 31 )),
840+ ControlLine ('for' , 'for k,v in d.items():' , False , (3 , 1 )),
841+ Text (' ' , (4 , 1 )),
842+ Expression ('k' , [], (4 , 13 )),
843+ Text (' = ' , (4 , 17 )),
844+ Expression ('v' , [], (4 , 20 )),
845+ Text ('\n ' , (4 , 24 )),
846+ ControlLine ('for' , 'endfor' , True , (5 , 1 )),
847+ Text (' ' , (6 , 1 ))
848+ ]
849+ ),
850+ Text ('\n \n ' , (6 , 16 )),
851+ CallNamespaceTag ('self:dtest' , {'d' : "${\n {\n 'id':'4',\n 'foo':'barr'\n }\n }" }, (8 , 9 ), []),
852+ Text ('\n ' , (13 , 30 ))
853+ ]
854+ )
855+ )
856+
857+ def test_dict_expression_2 (self ):
858+ template = """
859+ <%def name="thing(thing)">
860+ ${type(thing)}
861+ </%def>
862+
863+ <%self:thing thing="foo" />
864+
865+ <%self:thing thing="${5}" />
866+
867+ <%self:thing thing="${[1,2,3]}" />
868+
869+ <%self:thing thing="${{'id':'4'}}" />
870+
871+
872+ <%
873+ foo="this is foo"
874+ g=False
875+ %>
876+
877+ <%def name="bar(x, y)">
878+ ${x} ${y}
879+ </%def>
880+
881+ <%self:bar x=" ${{'id':4}} " y="x${g and '1' or '2'}y"/>
882+
883+ <%self:dtest d="${ {
884+ 'x-on:click':foo,
885+ 'foo':'bar'
886+ } }" />
887+ """
888+ nodes = Lexer (template ).parse ()
889+ self ._compare (
890+ nodes ,
891+ TemplateNode (
892+ {},
893+ [
894+ Text ('\n ' , (1 , 1 )),
895+ DefTag (
896+ 'def' ,
897+ {'name' : 'thing(thing)' },
898+ (2 , 9 ),
899+ [
900+ Text ('\n ' , (2 , 35 )),
901+ Expression ('type(thing)' , [], (3 , 13 )),
902+ Text ('\n ' , (3 , 27 ))
903+ ]
904+ ),
905+ Text ('\n \n ' , (4 , 16 )),
906+ CallNamespaceTag ('self:thing' , {'thing' : 'foo' }, (6 , 9 ), []),
907+ Text ('\n \n ' , (6 , 36 )),
908+ CallNamespaceTag ('self:thing' , {'thing' : '${5}' }, (8 , 9 ), []),
909+ Text ('\n \n ' , (8 , 37 )),
910+ CallNamespaceTag ('self:thing' , {'thing' : '${[1,2,3]}' }, (10 , 9 ), []),
911+ Text ('\n \n ' , (10 , 43 )),
912+ CallNamespaceTag ('self:thing' , {'thing' : "${{'id':'4'}}" }, (12 , 9 ), []),
913+ Text ('\n \n \n ' , (12 , 46 )),
914+ Code ('\n foo="this is foo"\n g=False\n \n ' , False , (15 , 9 )),
915+ Text ('\n \n ' , (18 , 11 )),
916+ DefTag (
917+ 'def' ,
918+ {'name' : 'bar(x, y)' },
919+ (20 , 9 ),
920+ [
921+ Text ('\n ' , (20 , 32 )),
922+ Expression ('x' , [], (21 , 13 )),
923+ Text (' ' , (21 , 17 )),
924+ Expression ('y' , [], (21 , 18 )),
925+ Text ('\n ' , (21 , 22 ))
926+ ]
927+ ),
928+ Text ('\n \n ' , (22 , 16 )),
929+ CallNamespaceTag ('self:bar' , {'x' : " ${{'id':4}} " , 'y' : "x${g and '1' or '2'}y" }, (24 , 9 ), []),
930+ Text ('\n \n ' , (24 , 65 )),
931+ CallNamespaceTag ('self:dtest' , {'d' : "${ {\n 'x-on:click':foo,\n 'foo':'bar'\n } }" }, (26 , 9 ), []),
932+ Text ('\n ' , (29 , 16 ))
933+ ]
934+ )
935+ )
936+
813937 def test_tricky_code (self ):
814938 template = """<% print('hi %>') %>"""
815939 nodes = Lexer (template ).parse ()
0 commit comments