Browse files

Support {% trans .. as .. %} tag (#131)

  • Loading branch information...
1 parent 0e5635c commit 4aa87879f9eefea0155bd98e40d5061096bc787d @kaos kaos committed Feb 26, 2014
Showing with 22 additions and 5 deletions.
  1. +11 −0 src/erlydtl_beam_compiler.erl
  2. +7 −4 src/erlydtl_parser.yrl
  3. +4 −1 tests/src/erlydtl_unittests.erl
View
11 src/erlydtl_beam_compiler.erl
@@ -631,6 +631,8 @@ body_ast(DjangoParseTree, TreeWalker) ->
widthratio_ast(Numerator, Denominator, Scale, TW);
({'with', Args, Contents}, TW) ->
with_ast(Args, Contents, TW);
+ ({'scope_as', {identifier, _, Name}, Contents}, TW) ->
+ scope_as(Name, Contents, TW);
({'extension', Tag}, TW) ->
extension_ast(Tag, TW);
({'extends', _}, _TW) ->
@@ -1138,6 +1140,15 @@ with_ast(ArgList, Contents, TreeWalker) ->
merge_info(ArgInfo, InnerInfo)},
restore_scope(TreeWalker1, TreeWalker2)}.
+scope_as(VarName, Contents, TreeWalker) ->
+ {{ContentsAst, ContentsInfo}, TreeWalker1} = body_ast(Contents, TreeWalker),
+ VarAst = merl:var(lists:concat(["Var_", VarName])),
+ {Id, TreeWalker2} = begin_scope(
+ [{VarName, VarAst}],
+ [?Q("_@VarAst = _@ContentsAst")],
+ TreeWalker1),
+ {{Id, ContentsInfo}, TreeWalker2}.
+
regroup_ast(ListVariable, GrouperVariable, LocalVarName, TreeWalker) ->
{{ListAst, ListInfo}, TreeWalker1} = value_ast(ListVariable, false, true, TreeWalker),
LocalVarAst = merl:var(lists:concat(["Var_", LocalVarName])),
View
11 src/erlydtl_parser.yrl
@@ -114,6 +114,7 @@ Nonterminals
BlockTransBlock
BlockTransContent
TransTag
+ TransText
TemplatetagTag
Templatetag
@@ -385,10 +386,12 @@ Templatetag -> closebrace_keyword : '$1'.
Templatetag -> opencomment_keyword : '$1'.
Templatetag -> closecomment_keyword : '$1'.
-TransTag -> open_tag trans_keyword string_literal close_tag : {trans, '$3'}.
-TransTag -> open_tag trans_keyword Variable close_tag : {trans, '$3'}.
-TransTag -> open_tag trans_keyword string_literal noop_keyword close_tag : '$3'.
-TransTag -> open_tag trans_keyword Variable noop_keyword close_tag : '$3'.
+TransTag -> open_tag trans_keyword TransText close_tag : {trans, '$3'}.
+TransTag -> open_tag trans_keyword TransText as_keyword identifier close_tag : {scope_as, '$5', [{trans, '$3'}]}.
+TransTag -> open_tag trans_keyword TransText noop_keyword close_tag : '$3'.
+
+TransText -> string_literal : '$1'.
+TransText -> Variable : '$1'.
WidthRatioTag -> open_tag widthratio_keyword Value Value number_literal close_tag : {widthratio, '$3', '$4', '$5'}.
View
5 tests/src/erlydtl_unittests.erl
@@ -1219,7 +1219,10 @@ tests() ->
<<"Hello Hi">>},
{"trans variable at run-time: No-op",
<<"Hello {% trans var1 noop %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
- <<"Hello Hi">>}
+ <<"Hello Hi">>},
+ {"trans as",
+ <<"{% trans 'Hans' as name %}Hello {{ name }}">>, [], <<"Hello Hans">>
+ }
]},
{"blocktrans",
[

0 comments on commit 4aa8787

Please sign in to comment.