How can I apply style to a div based on condition in thymeleaf?

前端 未结 2 1646
灰色年华
灰色年华 2021-01-11 23:26

I have a

block which I need to set to display:none or display:block based on the condition. The html looks like this,

2条回答
  •  广开言路
    2021-01-11 23:43

    Solved it while posting the question,

    th:style="${condition ? 'display:block' : 'display:none'}" >
    

    would produce the necessary conditional style. If condition is true display is set to block and none if condition is false.

    For admin,

    th:style="${role == 'ADMIN' ? 'display:block' : 'display:none'}" >
    

    the style is set to display:block and for other roles the block is not displayed.

提交回复
热议问题