Skip to content

Commit

Permalink
bug-fixes (now blocks can contain variables as well)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaccon committed Dec 10, 2007
1 parent c5c019e commit afbb1cd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 225 deletions.
4 changes: 2 additions & 2 deletions demo/out/test_extend.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
blastring
bar1string

base template

replacing the base title

more of base template

replacing the base content
replacing the base content - variable: bar2string after variable

end of base template
2 changes: 1 addition & 1 deletion demo/templates/test_extend.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% extends "base.html" %}
{% block title %}replacing the base title{% endblock %}
{% block content %}replacing the base content{% endblock %}
{% block content %}replacing the base content - variable: {{ another_variable }} after variable {% endblock %}
8 changes: 4 additions & 4 deletions src/demo/erlydtl_demo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ compile_templates() ->
%%--------------------------------------------------------------------
render_html() ->
OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
render(OutDir, test_variable, ".html", "foostring"),
render(OutDir, test_extend, ".html", "blastring"),
render(OutDir, test_variable, ".html", ["foostring"]),
render(OutDir, test_extend, ".html", ["bar1string", "bar2string"]),
render(OutDir, test_comment, ".html").


%%====================================================================
%% Internal functions
%%====================================================================

render(OutDir, Module, Ext, Var) ->
case catch Module:render(Var) of
render(OutDir, Module, Ext, Args) ->
case catch apply(Module, render, Args) of
{'EXIT', Reason} ->
io:format("TRACE ~p:~p ~p: rendering failure: ~n",[?MODULE, ?LINE, Reason]);
Val ->
Expand Down
64 changes: 33 additions & 31 deletions src/erlydtl/erlydtl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
%%%
%%% @since 2007-11-17 by Roberto Saccon
%%%-------------------------------------------------------------------
-module(erlydtl_api).
-module(erlydtl).
-author('rsaccon@gmail.com').

%% API
Expand Down Expand Up @@ -94,18 +94,15 @@ parse(File) ->

compile_reload_ast([H | T], ModuleName, FunctionName, RelDir) ->
{List, Args} = case transl(H, T, [], [], RelDir) of
{regular, List0, Args0} ->
io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, regualar]),
{[inplace_block(X) || X <- List0], Args0};
{inherited, List0, Arg0} ->
io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, inherited]),
{List0, Arg0}
end,
io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, {List, Args}]),
{regular, List0, Args0} ->
{[inplace_block(X) || X <- List0], Args0};
{inherited, List0, Arg0} ->
{List0, Arg0}
end,
Args2 = lists:reverse([{var, 1, Val} || {Val, _} <- Args]),
Cons = list_fold(lists:reverse(List)),
Ast2 = {function, 1, list_to_atom(FunctionName), length(Args2),
[{clause, 1, Args2, [], [Cons]}]},
[{clause, 1, Args2, [], [Cons]}]},
Ac = erlydtl_tools:create_module(Ast2 , ModuleName),
case compile:forms(Ac) of
{ok, Module, Bin} ->
Expand All @@ -126,23 +123,28 @@ list_fold([E1, E2]) ->
{cons, 1, E2, E1};
list_fold([E1, E2 | Tail]) ->
lists:foldl(fun(X, T) ->
{cons, 1, X, T}
end, {cons, 1, E2, E1}, Tail).
{cons, 1, X, T}
end, {cons, 1, E2, E1}, Tail).


transl(nil, [{extends, _, Name}], Out, Args, RelDir) ->
case parse(filename:join([RelDir, Name])) of
{ok, ParentAst} ->
[H|T]=ParentAst,
{_, List, Args1} = transl(H, T, [], [], RelDir),
{inherited, [replace_block(X, Out) || X <- List], Args1};
{error, Msg} ->
io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, Msg]),
io:format("TRACE ~p:~p Parent Parser failure: ~p~n",[?MODULE, ?LINE, Name]),
{regular, Out, Args}
{ok, ParentAst} ->
[H|T]=ParentAst,
{_, List, Args1} = transl(H, T, [], [], RelDir),
{List3, Args3} = lists:foldl(fun(X, {List2, Args2}) ->
{List4, Args4} = replace_block(X, Out, Args2),
{[List4 | List2], Args4}
end, {[], Args1}, List),
{inherited, lists:reverse(lists:flatten([List3])), lists:flatten(Args3)};
{error, Msg} ->
io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, Msg]),
io:format("TRACE ~p:~p Parent Parser failure: ~p~n",[?MODULE, ?LINE, Name]),
{regular, Out, Args}
end;

transl(nil, [{var, L, Val}], Out, Args, _) ->
io:format("TRACE ~p:~p nil ~p~n",[?MODULE, ?LINE, {Val, Args}]),
case lists:keysearch(Val, 2, Args) of
false ->
Key = list_to_atom(lists:concat(["A", length(Args) + 1])),
Expand All @@ -157,31 +159,31 @@ transl(nil, [Token], Out, Args, _) ->
transl([H | T], [{var, L, Val}], Out, Args, DocRoot) ->
case lists:keysearch(Val, 2, Args) of
false ->
io:format("TRACE ~p:~p normal_not_found ~p~n",[?MODULE, ?LINE, {Val, Args}]),
Key = list_to_atom(lists:concat(["A", length(Args) + 1])),
transl(H, T, [{var, L, Key} | Out], [{Key, Val} | Args], DocRoot);
{value, {Key, _}} ->
io:format("TRACE ~p:~p normal_found ~p~n",[?MODULE, ?LINE, {Val, Key, Args}]),
transl(H, T, [{var, L, Key} | Out], Args, DocRoot)
end;

transl([H | T], [Token], Out, Args, DocRoot) ->
transl(H, T, [Token | Out], Args, DocRoot).


replace_block({block, Name, [nil, Str1]}, List) ->
replace_block({block, Name, [nil, Val]}, List, Args) ->
case lists:keysearch(Name, 2, List) of
false ->
Str1;
{value, {_, _, [nil, Str2]}} ->
Str2
{Val, Args};
{value, {_, _, [H | T]}} ->
{_, List2, Args2} = transl(H, T, [], Args, undefined),
{lists:reverse(List2), Args2}
end;
replace_block(Other, _) ->
Other.

replace_block(Other, _, Args) ->
{Other, Args}.

inplace_block({block, _, [nil, Str]}) ->
Str;
inplace_block(Other) ->
Other.



Other.
187 changes: 0 additions & 187 deletions src/erlydtl/erlydtl_api.erl

This file was deleted.

0 comments on commit afbb1cd

Please sign in to comment.