Skip to content

Making parent template fragments optional #248

@andylaw

Description

@andylaw

I have a base template that drives multiple child pages. These pages all share a common header section, but then there are two sections just below that header that may or may not be required in the child.

<div class="header-container">
  <div class="common-header">
    ...
  </div>
  <div class="part1" layout:fragment="optional-1">Optional 1</div>
  <div class="part2" layout:fragment="optional-2">Optional 2</div>
</div>

If my child template does not have a layout:fragment called "optional-1", then I want the div with class "part1" to not be present in the output. So:

  <div class="part2" layout:fragment="optional-2">This from the child template</div>

Should result in:

<div class="header-container">
  <div class="common-header">
    ...
  </div>
   <div class="part2">This from the child template</div>
</div>

... with the "Optional 1" div completely removed.

The only way I can get this to work is to add a th:remove="all" to the base template, and a corresponding th:remove="none" to all of the child templates (because th:remove="all" without the corresponding "none" in the child template blows away the replaced tag completely)

Firstly, am I missing something obvious?

Secondly, am I weird for finding the need to add an effective manual override in all the child templates feels wrong and inefficient?

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions