2014-02-25 34 views
14

比方说,我有两个Thymeleaf的模板:在不包含片段定义元素的情况下使用Thymeleaf模板?

的index.html

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <header>foo</header> 
    <section> 
    <div th:replace="fragments/main :: content"></div> 
    </section> 
    <footer>bar</footer> 
</body> 
</html> 

片段/ main.html中

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <div th:fragment="content"> 
    <p>This is the main content.</p> 
    </div> 
</body> 
</html> 

我如何防止Tymeleaf从包括div定义合成输出中的片段?也就是说,如何获取Thymleaf生成以下的输出:

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <header>foo</header> 
    <section> 
    <p>This is the main content.</p> 
    </section> 
    <footer>bar</footer> 
</body> 
</html> 

相反的:

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <header>foo</header> 
    <section> 
    <div> 
    <p>This is the main content.</p> 
    </div> 
    </section> 
    <footer>bar</footer> 
</body> 
</html> 

回答

23

使用th:remove="tag"。 (documentation
片段/ main.html中:

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <div th:fragment="content" th:remove="tag"> 
    <p>This is the main content.</p> 
    </div> 
</body> 
</html> 

HTH

+0

美丽。谢谢。我会尽快接受它。 –

25

或者,你可以在main.html尝试使用th:block代替div,就像这样:

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <th:block th:fragment="content"> 
    <p>This is the main content.</p> 
    </th:block> 
</body> 
</html> 

注,但是,这将稍微改变main.html在查看原始HTML时的样子没有Thymeleaf预处理。

+1

我会给出一个+1。这是'th:block'的一个很好的用法。 –

相关问题