Browse Source

fix multiple includes which uses extends with blocks of the same name.

fixes #176.
Andreas Stenius 10 years ago
parent
commit
4f5fd21f60

+ 2 - 0
NEWS.md

@@ -9,3 +9,5 @@ Standards](http://www.gnu.org/prep/standards/html_node/NEWS-File.html#NEWS-File)
 * Fix issue with generated code for `for` loops (#167).
 
 * Fix issue with using keywords as attributes (#177).
+
+* Fix issue when including multiple templates extending a common base template (#176).

+ 8 - 3
src/erlydtl_beam_compiler.erl

@@ -63,8 +63,9 @@
          empty_scope/0, get_current_file/1, add_errors/2,
          add_warnings/2, merge_info/2, call_extension/3,
          init_treewalker/1, resolve_variable/2, resolve_variable/3,
-         reset_parse_trail/2, load_library/3, load_library/4,
-         shorten_filename/2, push_auto_escape/2, pop_auto_escape/1]).
+         reset_block_dict/2, reset_parse_trail/2, load_library/3,
+         load_library/4, shorten_filename/2, push_auto_escape/2,
+         pop_auto_escape/1]).
 
 -include_lib("merl/include/merl.hrl").
 -include("erlydtl_ext.hrl").
@@ -539,7 +540,11 @@ body_ast([{'extends', {string_literal, _Pos, String}} | ThisParseTree], #treewal
                                                           parse_trail = [File | Context#dtl_context.parse_trail]
                                                          }
                                                })),
-                    {Info, reset_parse_trail(Context#dtl_context.parse_trail, TreeWalker1)};
+                    {Info, reset_parse_trail(
+                             Context#dtl_context.parse_trail,
+                             reset_block_dict(
+                               Context#dtl_context.block_dict,
+                               TreeWalker1))};
                 {error, Reason} ->
                     empty_ast(?ERR(Reason, TreeWalker))
             end

+ 6 - 0
src/erlydtl_compiler_utils.erl

@@ -63,6 +63,7 @@
          merge_info/2,
          print/3, print/4,
          push_scope/2,
+         reset_block_dict/2,
          reset_parse_trail/2,
          resolve_variable/2, resolve_variable/3,
          restore_scope/2,
@@ -230,6 +231,11 @@ end_scope(Fun, Id, AstList, TreeWalker) ->
 
 empty_scope() -> {[], []}.
 
+reset_block_dict(BlockDict, #treewalker{ context=Context }=TreeWalker) ->
+    TreeWalker#treewalker{ context=reset_block_dict(BlockDict, Context) };
+reset_block_dict(BlockDict, Context) ->
+    Context#dtl_context{ block_dict=BlockDict }.
+
 reset_parse_trail(ParseTrail, #treewalker{ context=Context }=TreeWalker) ->
     TreeWalker#treewalker{ context=reset_parse_trail(ParseTrail, Context) };
 reset_parse_trail(ParseTrail, Context) ->

+ 6 - 2
test/erlydtl_test_defs.erl

@@ -1652,7 +1652,7 @@ all_test_defs() ->
       end},
      {"functional",
       [functional_test(F)
-       %% order is important.
+       %% order is important for a few of these tests, unfortunately.
        || F <- ["autoescape", "comment", "extends", "filters", "for", "for_list",
                 "for_tuple", "for_list_preset", "for_preset", "for_records",
                 "for_records_preset", "include", "if", "if_preset", "ifequal",
@@ -1662,7 +1662,7 @@ all_test_defs() ->
                 "include_template", "include_path", "ssi", "extends_path",
                 "extends_path2", "trans", "extends_for", "extends2", "extends3",
                 "recursive_block", "extend_recursive_block", "missing",
-                "block_super"]
+                "block_super", "wrapper"]
       ]},
      {"compile_dir",
       [setup_compile(T)
@@ -1943,6 +1943,10 @@ setup("custom_tag4") ->
 setup("ssi") ->
     RenderVars = [{path, "ssi_include.html"}],
     {ok, RenderVars};
+setup("wrapper") ->
+    RenderVars = [{types, ["b", "a", "c"]}],
+    {ok, RenderVars};
+
 %%--------------------------------------------------------------------
 %% Custom tags
 %%--------------------------------------------------------------------

+ 49 - 0
test/files/expect/wrapper

@@ -0,0 +1,49 @@
+
+    
+        including base b now
+        
+
+base template
+
+base b
+
+more of base template
+
+base content
+
+end of base template
+
+    
+
+    
+        including base a now
+        
+
+base template
+
+base a
+
+more of base template
+
+base content
+
+end of base template
+
+    
+
+    
+        including base c now
+        
+
+base template
+
+base c
+
+more of base template
+
+base content
+
+end of base template
+
+    
+

+ 3 - 0
test/files/input/base_a

@@ -0,0 +1,3 @@
+{% extends "base" %}
+
+{% block title %}base a{% endblock %}

+ 3 - 0
test/files/input/base_b

@@ -0,0 +1,3 @@
+{% extends "base" %}
+
+{% block title %}base b{% endblock %}

+ 3 - 0
test/files/input/base_c

@@ -0,0 +1,3 @@
+{% extends "base" %}
+
+{% block title %}base c{% endblock %}

+ 12 - 0
test/files/input/wrapper

@@ -0,0 +1,12 @@
+{% for type in types %}
+    {% if type=="a" %}
+        including base a now
+        {% include "base_a" %}
+    {% elif type=="b" %}
+        including base b now
+        {% include "base_b" %}
+    {% else %}
+        including base c now
+        {% include "base_c" %}
+    {% endif %}
+{% endfor %}