ultraq / thymeleaf-layout-dialect

A dialect for Thymeleaf that lets you build layouts and reusable templates in order to improve code reuse

Home Page:https://ultraq.github.io/thymeleaf-layout-dialect/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conditionals in layout:decorate

TreyBastian opened this issue · comments

commented

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.

commented

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)}">