Error on if/else condition

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Error on if/else condition

apprentice
I'm trying to add a condition to the outer div but getting error on line: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 61; Element type "div" must be followed by either attribute specifications, ">" or "/>".

Here is the div

<div th:if="${account.image != null}" ? class="photo" th:style="background: url('/user/image/photos/' + ${account.image.name}+ ') 0% 0% / 100% 100% no-repeat;'" : class="photo" >
....
</div>

What the above line should do is if variable 'image' is not null then it would display the div with the style and 'cover' class, if variable is null then div will have the class 'photo' only.
 

Reply | Threaded
Open this post in threaded view
|

Re: Error on if/else condition

Metroids
th:if doesn't work like that... it applies to the entire div tag, and not to the individual attributes after it.  You definitely can't do a ternary in the html itself like that (which is causing the xml parsing error).

This should work for what you want:

<div th:classappend="${account.image != null ? 'cover' : 'photo'}" th:style="${account.image != null ? 'background: url(/user/image/photos/' + account.image.name + ') 0% 0% / 100% 100% no-repeat;' : ''}" />
Reply | Threaded
Open this post in threaded view
|

Re: Error on if/else condition

apprentice
Thanks