Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix django compatibility for custom tag arguments.

Fixes #78.

Arguments to custom tags can now be any number of values and/or key-value pairs.
  • Loading branch information...
commit c065dc12488dfecfd0407b46b60f4fb2d52a6f06 1 parent 5464e17
@kaos kaos authored
View
18 src/erlydtl_compiler.erl
@@ -1481,17 +1481,19 @@ full_path(File, DocRoot) ->
%% Custom tags
%%-------------------------------------------------------------------
+interpret_value({trans, StringLiteral}, Context, TreeWalker) ->
+ translated_ast(StringLiteral, Context, TreeWalker);
+interpret_value(Value, Context, TreeWalker) ->
+ value_ast(Value, false, false, Context, TreeWalker).
+
interpret_args(Args, Context, TreeWalker) ->
lists:foldr(
- fun ({{identifier, _, Key}, {trans, StringLiteral}}, {{ArgsAcc, AstInfoAcc}, TreeWalkerAcc}) ->
- {{TransAst, TransAstInfo}, TreeWalker0} = translated_ast(StringLiteral, Context, TreeWalkerAcc),
- {{[erl_syntax:tuple([erl_syntax:atom(Key), TransAst])|ArgsAcc], merge_info(TransAstInfo, AstInfoAcc)}, TreeWalker0};
- ({{identifier, _, Key}, Value}, {{ArgsAcc, AstInfoAcc}, TreeWalkerAcc}) ->
- {{Ast0, AstInfo0}, TreeWalker0} = value_ast(Value, false, false, Context, TreeWalkerAcc),
+ fun ({{identifier, _, Key}, Value}, {{ArgsAcc, AstInfoAcc}, TreeWalkerAcc}) ->
+ {{Ast0, AstInfo0}, TreeWalker0} = interpret_value(Value, Context, TreeWalkerAcc),
{{[erl_syntax:tuple([erl_syntax:atom(Key), Ast0])|ArgsAcc], merge_info(AstInfo0, AstInfoAcc)}, TreeWalker0};
- ({extension, Tag}, {{ArgsAcc, AstInfoAcc}, TreeWalkerAcc}) ->
- {{ExtAst, ExtInfo}, ExtTreeWalker} = extension_ast(Tag, Context, TreeWalkerAcc),
- {{[ExtAst|ArgsAcc], merge_info(ExtInfo, AstInfoAcc)}, ExtTreeWalker}
+ (Value, {{ArgsAcc, AstInfoAcc}, TreeWalkerAcc}) ->
+ {{Ast0, AstInfo0}, TreeWalker0} = value_ast(Value, false, false, Context, TreeWalkerAcc),
+ {{[Ast0|ArgsAcc], merge_info(AstInfo0, AstInfoAcc)}, TreeWalker0}
end, {{[], #ast_info{}}, TreeWalker}, Args).
tag_ast(Name, Args, Context, TreeWalker) ->
View
7 src/erlydtl_parser.yrl
@@ -100,6 +100,7 @@ Nonterminals
EndIfNotEqualBraced
CustomTag
+ CustomArgs
Args
RegroupBlock
@@ -388,7 +389,11 @@ Filter -> identifier ':' Variable : ['$1', '$3'].
Literal -> string_literal : '$1'.
Literal -> number_literal : '$1'.
-CustomTag -> open_tag identifier Args close_tag : {tag, '$2', '$3'}.
+CustomTag -> open_tag identifier CustomArgs close_tag : {tag, '$2', '$3'}.
+
+CustomArgs -> '$empty' : [].
+CustomArgs -> identifier '=' Value CustomArgs : [{'$1', '$3'}|'$4'].
+CustomArgs -> Value CustomArgs : ['$1'|'$2'].
Args -> '$empty' : [].
Args -> Args identifier '=' Value : '$1' ++ [{'$2', '$4'}].
View
5 tests/src/erlydtl_custom_tags.erl
@@ -11,6 +11,5 @@ custom2([], _RenderOptions = [{locale, ru}, {foo, bar}]) ->
custom3([], _RenderOptions = [{locale, ru}]) ->
<<"b3">>.
-custom4(TagVars) ->
- Lst = [binary_to_list(X) || X <- TagVars],
- string:join(Lst, "|").
+custom4(_TagVars = [<<"a">>]) ->
+ <<"a">>.
View
4 tests/src/erlydtl_functional_tests.erl
@@ -173,7 +173,7 @@ setup("custom_tag2") ->
setup("custom_tag3") ->
{ok, [{a, <<"a1">>}], [{locale, ru}], <<"b3\n">>};
setup("custom_tag4") ->
- {ok, [], [], [<<"a|b">>, <<"\n">>]};
+ {ok, [], [], <<"a\n">>};
setup("ssi") ->
RenderVars = [{path, filename:absname(filename:join(["tests", "input", "ssi_include.html"]))}],
{ok, RenderVars};
@@ -301,7 +301,7 @@ test_render(Name, Module) ->
io:format("failed"),
{error, io_lib:format(
"Expected output does not match rendered output~n"
- "==Expected==~n~s~n--Actual--~n~s~n==End==~n",
+ "==Expected==~n~p~n--Actual--~n~p~n==End==~n",
[RenderResult, Data])}
end;
true ->
Please sign in to comment.
Something went wrong with that request. Please try again.