Skip to content

Commit

Permalink
Fix for #30, pass the correct fragments up the page hierarchy.
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Dec 19, 2013
1 parent 0e5846c commit 1329fb9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
20 changes: 12 additions & 8 deletions Java/nz/net/ultraq/thymeleaf/decorator/DecoratorProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package nz.net.ultraq.thymeleaf.decorator;

import nz.net.ultraq.thymeleaf.AbstractContentProcessor;

import static nz.net.ultraq.thymeleaf.FragmentProcessor.PROCESSOR_NAME_FRAGMENT;
import static nz.net.ultraq.thymeleaf.LayoutDialect.DIALECT_PREFIX_LAYOUT;
import static nz.net.ultraq.thymeleaf.LayoutUtilities.*;
Expand All @@ -34,6 +33,7 @@
import org.thymeleaf.standard.fragment.StandardFragmentProcessor;

import java.util.Map;
import java.util.Set;

/**
* Specifies the name of the decorator template to apply to a content template.
Expand Down Expand Up @@ -96,22 +96,26 @@ protected ProcessorResult processAttribute(Arguments arguments, Element element,
Document decoratordocument = decoratortemplate.getDocument();
Element decoratorrootelement = decoratordocument.getFirstElementChild();

// Gather all fragment parts from this page and scope to the HTML element.
// These will be used to decorate the document as Thymeleaf encounters the
// fragment placeholders.
// Gather all fragment parts from this page
Map<String,Object> fragments = findFragments(document.getElementChildren());
if (!fragments.isEmpty()) {
decoratorrootelement.setAllNodeLocalVariables(fragments);
}

// Decide which kind of decorator to apply, given the decorator page root element
Decorator decorator = decoratorrootelement != null &&
decoratorrootelement.getOriginalName().equals(HTML_ELEMENT_HTML) ?
new HtmlDocumentDecorator() :
new XmlDocumentDecorator();

// Perform decoration
// Perform decoration, apply any new fragments found from the pre-decorated page
decorator.decorate(decoratorrootelement, document.getFirstElementChild());
if (!fragments.isEmpty()) {
Element newrootelement = document.getFirstElementChild();
Set<String> nodelocalvariables = newrootelement.getNodeLocalVariableNames();
for (String fragmentname: fragments.keySet()) {
if (!nodelocalvariables.contains(fragmentname)) {
newrootelement.setNodeLocalVariable(fragmentname, fragments.get(fragmentname));
}
}
}

return ProcessorResult.OK;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,13 @@ public void nonXmlHtml5() {

testOK("decorator/NonXMLHTML5");
}

/**
* Test a deep layout hierarchy (3 levels).
*/
@Test
public void deepHierarchy() {

testOK("decorator/DeepHierarchy");
}
}
74 changes: 74 additions & 0 deletions Tests/nz/net/ultraq/thymeleaf/tests/decorator/DeepHierarchy.thtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
%TEMPLATE_MODE HTML5

%INPUT
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
layout:decorator="Parent">
<head>
<title>Page title</title>
<script src="child-script.js"></script>
</head>
<body>
<div layout:fragment="content">
<p>This is a paragraph from the child page</p>
</div>
</body>
</html>

%INPUT[Parent]
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="Grandparent">
<body>
<section layout:fragment="section">
<header>
<h1>My website</h1>
</header>
<div layout:fragment="content">
<p>Page content goes here</p>
</div>
<footer>
<p>My footer</p>
</footer>
</section>
</body>
</html>

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

%OUTPUT
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page title</title>
<script src="grandparent-script.js"></script>
<script src="child-script.js"></script>
</head>
<body>
<section>
<header>
<h1>My website</h1>
</header>
<div>
<p>This is a paragraph from the child page</p>
</div>
<footer>
<p>My footer</p>
</footer>
</section>
</body>
</html>
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ project.sourceCompatibility = '1.6'

project.group = 'nz.net.ultraq.thymeleaf'
project.artifact = 'thymeleaf-layout-dialect'
project.version = '1.2'
project.version = '1.2.1-SNAPSHOT'
project.year = '2012'

dependencies {
compile(
'org.thymeleaf:thymeleaf:2.1.0.RELEASE'
'org.thymeleaf:thymeleaf:2.1.2.RELEASE'
)
testCompile(
'junit:junit:4.11',
'org.thymeleaf:thymeleaf-testing:2.1.0.RELEASE'
'org.thymeleaf:thymeleaf-testing:2.1.2.RELEASE'
)
testRuntime(
'net.sourceforge.nekohtml:nekohtml:1.9.18'
Expand Down

0 comments on commit 1329fb9

Please sign in to comment.