Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf 3 - multiple ternary evaluations possible?

Tags:

thymeleaf

Does Thymeleaf 3 support multiple ternary conditions, such as:

cond1 ? do1 : cond2 ? do2 : do3

The following code works in Thymeleaf 3:

<th:block th:replace="${toolbar == true} ? ~{common/fragments :: _$pageToolbar} :  ~{app/fragments :: ${toolbar}}">

Where I'm passing trueto get the default toolbar or a string to get a custom toolbar.

But this code, which should essentially remove the block if nothing is passed in the toolbar parameter generates a parsing exception:

<th:block th:replace="${toolbar == true} ? ~{common/fragments :: _$pageToolbar} : ${toolbar != null} ? ~{app/fragments :: ${toolbar}} : ~{common/fragments :: _$empty}">

Maybe I'm taking the wrong approach here?

The Thymeleaf parsing exception:

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "${toolbar == true} ? ~{common/fragments :: _$pageToolbar} : ${toolbar != null} ? ~{app/fragments :: ${toolbar}} : ~{common/fragments :: _$empty}" (template: "common/fragments" - line 70, col 15)
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:131) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:62) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:44) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardFragmentInsertionTagProcessor.computeFragment(AbstractStandardFragmentInsertionTagProcessor.java:387) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardFragmentInsertionTagProcessor.doProcess(AbstractStandardFragmentInsertionTagProcessor.java:111) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.Model.process(Model.java:282) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1587) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
... 79 common frames omitted
like image 977
rayduels Avatar asked Jul 26 '17 15:07

rayduels


1 Answers

Does Thymeleaf 3 support multiple ternary conditions, such as:

cond1 ? do1 : cond2 ? do2 : do3

Yes, it does. Just enclose the second ternary operator with brackets to get rid of parse exception.

cond1 ? do1 : (cond2 ? do2 : do3)

Your expression should work after applying that.

like image 62
Jakub Ch. Avatar answered Sep 24 '22 23:09

Jakub Ch.