ultraq / thymeleaf-layout-dialect

A dialect for Thymeleaf that lets you build layouts and reusable templates in order to improve code reuse
https://ultraq.github.io/thymeleaf-layout-dialect/
Apache License 2.0
701 stars 112 forks source link

Conditionals in layout:decorate #210

Closed TreyBastian closed 3 years ago

TreyBastian commented 3 years ago

I've tried a whole bunch of different forms to get a if(true) x else y conditional in a layout:decorate field to no avail. Is this dooable?

ultraq commented 3 years ago

Yes, this should be doable. The layout dialect leans on Thymeleaf's fragment selector syntax, which allows for the full expression to be used in it, including conditionals. See: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#fragment-specification-syntax

I was able to confirm the templateName part of the syntax in a test by doing something like this:

<html layout:decorate="~{${true} ? 'true-template.html' : 'false-template.html'}">

If I switch the true for false then the result would be decorating the other template.

TreyBastian commented 3 years ago

Awesome. I just figured out where I was going wrong with this.

I was passing parameters into one layout and defining the conditions with strings like you did, but including parameters in one of my template calls within the string.

<html layout:decorate="~{${false} ? 'layouts/true' : 'layouts/false(someOption=true)'}">

If I moved the parameters outside of the string defining the template it worked.

<html layout:decorate="~{${false} ? 'layouts/true' : 'layouts/false'(someOption=true)}">