Conditionals in layout:decorate
TreyBastian opened this issue · comments
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?
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.
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)}">