Skip to content

Commit

Permalink
WIP - Fixes for 200/201?
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Apr 10, 2020
1 parent 75c51d1 commit 2290e9b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 5 deletions.
6 changes: 5 additions & 1 deletion source/nz/net/ultraq/thymeleaf/fragments/FragmentMap.groovy
Expand Up @@ -49,14 +49,18 @@ class FragmentMap extends HashMap<String,List<IModel>> {
* @param context
* @param structureHandler
* @param fragments The new fragments to add to the map.
* @param isIncludeProcessing
*/
static void setForNode(IContext context, IElementModelStructureHandler structureHandler,
Map<String,List<IModel>> fragments) {
Map<String,List<IModel>> 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
Expand Down
Expand Up @@ -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)

Expand Down
Expand Up @@ -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.
Expand Down
Expand Up @@ -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.
Expand Down
Expand Up @@ -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.
Expand Down
@@ -0,0 +1,89 @@

# Test a deep layout hierarchy (3 levels).

%TEMPLATE_MODE HTML


%INPUT
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{Parent}">
<head>
<title>Page title</title>
<script src="child-script.js"></script>
</head>
<body>
<div layout:fragment="content">
<p>Page content</p>
</div>
<footer layout:fragment="footer">
<p>Page footer</p>
</footer>
</body>
</html>


%INPUT[Parent]
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{Grandparent}">
<head>
<script src="parent-script.js"></script>
</head>
<body>
<section layout:fragment="section">
<header>
<h1>My website</h1>
</header>
<div layout:fragment="content">
<p>Parent content</p>
</div>
</section>
<!-- parent redeclares footer -->
<footer layout:fragment="footer">
<p>Parent footer</p>
</footer>
</body>
</html>


%INPUT[Grandparent]
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<script src="grandparent-script.js"></script>
</head>
<body>
<section layout:fragment="section">
<p>Grandparent section</p>
</section>
<footer layout:fragment="footer">
<p>Grandparent footer</p>
</footer>
</body>
</html>


%OUTPUT
<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
<script src="grandparent-script.js"></script>
<script src="parent-script.js"></script>
<script src="child-script.js"></script>
</head>
<body>
<section>
<header>
<h1>My website</h1>
</header>
<div>
<p>Page content</p>
</div>
</section>
<footer>
<p>Page footer</p>
</footer>
</body>
</html>

0 comments on commit 2290e9b

Please sign in to comment.