Skip to content

Commit

Permalink
Bug fix in saving template containing custom_tags_modules feature
Browse files Browse the repository at this point in the history
* Bug fix: when compiling templates with custom tags with a given
custom_tags_modules option, the arguments are saved as atoms in the
AST as opposed to strings. This causes a failure when decompiling
the beam and printing its source by erl_prettypr.

In order to illustrate the issue compile the template test.dtl containing:

    {% tr xxx="ABC" %}

with options:

    [
        , {doc_root, "src"}
        , {out_dir,   "ebin"}
        , {compiler_options, [verbose, debug_info, report, return]}
        , {custom_tags_modules, [custom_tags]}
        , {force_recompile, true}
    ].

where custom_tags module is:

    -module(custom_tags).

    -export([tr/2]).

    tr(Vars, Context) ->
        io:format("Vars: ~p\n  Context: ~p\n", [Vars, Context]),
        [].

Observe the return code of erlydtl_compiler:compile/3, and also try to
decompile the emitted beam with:
[http://erlang.org/pipermail/erlang-questions/2006-January/018813.html]
  • Loading branch information
saleyn committed Oct 26, 2012
1 parent f74b94f commit 15a46cd
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/erlydtl_compiler.erl
Expand Up @@ -1237,17 +1237,22 @@ full_path(File, DocRoot) ->
%% Custom tags
%%-------------------------------------------------------------------

key_to_string(Key) when is_atom(Key) ->
erl_syntax:string(atom_to_list(Key));
key_to_string(Key) when is_list(Key) ->
erl_syntax:string(Key).

tag_ast(Name, Args, Context, TreeWalker) ->
{InterpretedArgs, AstInfo} = lists:mapfoldl(fun
({{identifier, _, Key}, {string_literal, _, Value}}, AstInfoAcc) ->
{{StringAst, StringAstInfo}, _} = string_ast(unescape_string_literal(Value), Context, TreeWalker),
{erl_syntax:tuple([erl_syntax:string(Key), StringAst]), merge_info(StringAstInfo, AstInfoAcc)};
{erl_syntax:tuple([key_to_string(Key), StringAst]), merge_info(StringAstInfo, AstInfoAcc)};
({{identifier, _, Key}, {trans, StringLiteral}}, AstInfoAcc) ->
{{TransAst, TransAstInfo}, _} = translated_ast(StringLiteral, Context, TreeWalker),
{erl_syntax:tuple([erl_syntax:string(Key), TransAst]), merge_info(TransAstInfo, AstInfoAcc)};
{erl_syntax:tuple([key_to_string(Key), TransAst]), merge_info(TransAstInfo, AstInfoAcc)};
({{identifier, _, Key}, Value}, AstInfoAcc) ->
{AST, VarName} = resolve_variable_ast(Value, Context),
{erl_syntax:tuple([erl_syntax:string(Key), format(AST,Context, TreeWalker)]), merge_info(#ast_info{var_names=[VarName]}, AstInfoAcc)}
{erl_syntax:tuple([key_to_string(Key), format(AST,Context, TreeWalker)]), merge_info(#ast_info{var_names=[VarName]}, AstInfoAcc)}
end, #ast_info{}, Args),

{RenderAst, RenderInfo} = custom_tags_modules_ast(Name, InterpretedArgs, Context),
Expand Down

0 comments on commit 15a46cd

Please sign in to comment.