diff --git a/source/nz/net/ultraq/thymeleaf/fragments/FragmentMap.groovy b/source/nz/net/ultraq/thymeleaf/fragments/FragmentMap.groovy index cc277ccfa..d78290d46 100644 --- a/source/nz/net/ultraq/thymeleaf/fragments/FragmentMap.groovy +++ b/source/nz/net/ultraq/thymeleaf/fragments/FragmentMap.groovy @@ -49,14 +49,18 @@ class FragmentMap extends HashMap> { * @param context * @param structureHandler * @param fragments The new fragments to add to the map. + * @param isIncludeProcessing */ static void setForNode(IContext context, IElementModelStructureHandler structureHandler, - Map> fragments) { + Map> fragments, boolean isIncludeProcessing = false) { structureHandler.setLocalVariable(FRAGMENT_COLLECTION_KEY, get(context).inject(fragments.clone()) { accumulator, fragmentName, fragmentList -> if (accumulator[fragmentName]) { accumulator[fragmentName] += fragmentList + if (isIncludeProcessing) { + accumulator[fragmentName].reverse(true) + } } else { accumulator[fragmentName] = fragmentList diff --git a/source/nz/net/ultraq/thymeleaf/fragments/FragmentProcessor.groovy b/source/nz/net/ultraq/thymeleaf/fragments/FragmentProcessor.groovy index e499b40ec..fb06da624 100644 --- a/source/nz/net/ultraq/thymeleaf/fragments/FragmentProcessor.groovy +++ b/source/nz/net/ultraq/thymeleaf/fragments/FragmentProcessor.groovy @@ -84,7 +84,7 @@ class FragmentProcessor extends AbstractAttributeTagProcessor { // Replace the tag body with the fragment if (fragments) { - def fragment = fragments.first() + def fragment = fragments.last() def modelFactory = context.modelFactory def replacementModel = new ElementMerger(context).merge(modelFactory.createModel(tag), fragment) diff --git a/source/nz/net/ultraq/thymeleaf/includes/IncludeProcessor.groovy b/source/nz/net/ultraq/thymeleaf/includes/IncludeProcessor.groovy index de5d8f595..9e59e734b 100644 --- a/source/nz/net/ultraq/thymeleaf/includes/IncludeProcessor.groovy +++ b/source/nz/net/ultraq/thymeleaf/includes/IncludeProcessor.groovy @@ -91,7 +91,7 @@ class IncludeProcessor extends AbstractAttributeModelProcessor { // Gather all fragment parts within the include element, scoping them to this element def includeFragments = new FragmentFinder(dialectPrefix).findFragments(model) - FragmentMap.setForNode(context, structureHandler, includeFragments) + FragmentMap.setForNode(context, structureHandler, includeFragments, true) // Keep track of what template is being processed? Thymeleaf does this for // its include processor, so I'm just doing the same here. diff --git a/source/nz/net/ultraq/thymeleaf/includes/InsertProcessor.groovy b/source/nz/net/ultraq/thymeleaf/includes/InsertProcessor.groovy index d5af61e0e..95a8c7738 100644 --- a/source/nz/net/ultraq/thymeleaf/includes/InsertProcessor.groovy +++ b/source/nz/net/ultraq/thymeleaf/includes/InsertProcessor.groovy @@ -72,7 +72,7 @@ class InsertProcessor extends AbstractAttributeModelProcessor { // Gather all fragment parts within this element, scoping them to this element def includeFragments = new FragmentFinder(dialectPrefix).findFragments(model) - FragmentMap.setForNode(context, structureHandler, includeFragments) + FragmentMap.setForNode(context, structureHandler, includeFragments, true) // Keep track of what template is being processed? Thymeleaf does this for // its include processor, so I'm just doing the same here. diff --git a/source/nz/net/ultraq/thymeleaf/includes/ReplaceProcessor.groovy b/source/nz/net/ultraq/thymeleaf/includes/ReplaceProcessor.groovy index 3db7660da..e05654b37 100644 --- a/source/nz/net/ultraq/thymeleaf/includes/ReplaceProcessor.groovy +++ b/source/nz/net/ultraq/thymeleaf/includes/ReplaceProcessor.groovy @@ -72,7 +72,7 @@ class ReplaceProcessor extends AbstractAttributeModelProcessor { // Gather all fragment parts within the include element, scoping them to this element def includeFragments = new FragmentFinder(dialectPrefix).findFragments(model) - FragmentMap.setForNode(context, structureHandler, includeFragments) + FragmentMap.setForNode(context, structureHandler, includeFragments, true) // Keep track of what template is being processed? Thymeleaf does this for // its include processor, so I'm just doing the same here. diff --git a/tests/nz/net/ultraq/thymeleaf/tests/decorators/Decorate-DeepHierarchy2.thtest b/tests/nz/net/ultraq/thymeleaf/tests/decorators/Decorate-DeepHierarchy2.thtest new file mode 100644 index 000000000..2aee5e33b --- /dev/null +++ b/tests/nz/net/ultraq/thymeleaf/tests/decorators/Decorate-DeepHierarchy2.thtest @@ -0,0 +1,89 @@ + +# Test a deep layout hierarchy (3 levels). + +%TEMPLATE_MODE HTML + + +%INPUT + + + + Page title + + + +
+

Page content

+
+
+

Page footer

+
+ + + + +%INPUT[Parent] + + + + + + +
+
+

My website

+
+
+

Parent content

+
+
+ +
+

Parent footer

+
+ + + + +%INPUT[Grandparent] + + + + + + +
+

Grandparent section

+
+
+

Grandparent footer

+
+ + + + +%OUTPUT + + + + Page title + + + + + +
+
+

My website

+
+
+

Page content

+
+
+ + +