SQL (Active column is of type bit):
id Question Active
1 Weather today 1
ASP.net Eval:
<img src='<%# Eval("Active") == "1" ? "images/active.png" : "images/inactive.png" %>' />
HTML:
<img src="images/inactive.png">
Why is the inactive.png image showing and not the active.
Bit fields correspond to boolean. Also you need to do a type conversion to ensure right comparison is done, as Eval outputs just object. So:
(bool)Eval("Active") == true
You could try to cast the result:
((int)Eval("Active")) == 1 ? [...]
or as mentioned in the comments to a bool:
((bool)Eval("Active")) == true ? [...]
Related
I'm actually making my discord bot and I wanted to make a command to know plane informations. I made it but know i want to put plane photos depending on the company.
I tried to do that:
if plane_data["data"][f"{flight_companie}"] == "FedEx" or "Ryanair" or "SWISS" or "Air France" or "SWISS" or "British Airways":
plane_photo = plane_data["data"][flight_companie]
else:
plane_photo = plane_data["data"]["unknown"]
Unknown is equal to a url photo that says there is no plane photos.
When i try with UPS it gives me that out:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'UPS Airlines'
Please help me out!!
if plane_data["data"][f"{flight_companie}"] == "FedEx" or "Ryanair"...
will always evaluate to True. This is because a string like "Ryanair" is truthy. Meaning it becomes true when converted to a boolean. So your line is equivialent to
if plane_data["data"][f"{flight_companie}"] == "FedEx" or True or True or True ...
or can not be used in this situation. I would use
if plane_data["data"][f"{flight_companie}"] in ["FedEx","Ryanair","SWISS","Air France","SWISS","British Airways"]:
instead.
I want to be able to read the value that I have set in the dialog and use it in Sightly to control what section of code is shown. When I have tried using the code below I have been receiving this error "Operands are not of the same type : comparison is supported for numbers only".
I have tried so many different fixes and have found nothing that works nor any documentation for it. Is context = 'number' not the correct syntax or is there anything else I have to add?
IN THE DIALOG
<number
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/select"
fieldLabel="Select Amount of Delivery Options"
name="./number"
value = "4" >
<items jcr:primaryType="nt:unstructured">
<four
jcr:primaryType="nt:unstructured"
text="Four"
value= "4" />
<three
jcr:primaryType="nt:unstructured"
text="Three"
value= "3" />
<two
jcr:primaryType="nt:unstructured"
text="Two"
value= "2" />
<one
jcr:primaryType="nt:unstructured"
text="One"
value= "1" />
</items> </number>
IN THE HTL
<sly data-sly-test="${properties.podnumber # context = 'number' >= 1}">
first, your dialog has name="./number and in your HTL you use properties.podnumber they do not match.
To answer your question: there is no way to do this with sightly alone, the context option is only for rendering (XSS protection) and does not change the value.
Your best bet is to use a sling model, something like
I assume your dialog will have name="podNumber"
#Model(
adaptables = {Resource.class},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public interface MyModel {
#Inject
int getPodNumber();
}
Sling will then convert that value to an integer you can use in your comparison. so you can add your model with data-sly-use.myModel="package.name.MyModel" then use it:
<sly data-sly-test="${myModel.podNumber >= 1}">
By the way, all the values in your dropdown are larger than or equal to 1.
NOTE: as Florian suggested in the comment below, you should use boolean checks in the model, instead of having to compare values in HTL. for example:
#Model(
adaptables = {Resource.class},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MyModel {
#Inject
int podNumber;
boolean isLargerThanOne(){
return podNumber > 1;
}
I want to print the address in the following order and I was able to do that. My problem is for some users some address fields are null then it is printing like this (1804 E Broadway, , 223344) and also I want divide the address in to lines.
<td>${addr1 }, ${addr2 }, ${postalCode }</td>
You can achieve your requirement is 3 ways:
1. JSP EL
<!-- here I am using adjacent EL expression because string concat will not work here and if we put EL expression in new line one space is adding-->
<td>${addr1 }${empty addr1? '': ','}
${addr2 }${empty addr2? '': ','}
${postalCode }</td>
2. JSTL
<c:if test="${not empty addr1}">
${addr1},
</c:if>
3. Using java code in JSP
I will not recommend this method because writing java code in JSP is not encouraged.
<%= (addr1 != null && addr1 !="") ? addr1 +",": "" %>
You can use a ternary operator in your JSP like this:
<%= (field != null && field !="") ? field +",": "" %>
I'm trying to use an expression in a Derived Column Transformation and it won't change from red, below is my code and I'm sure I have it correct, yet it keeps failing on me, any ideas why please?
(DT_WSTR,100,1252)([CategoryName]==''16-24s'' ? ''1'' :([CategoryName] ==''Boys''? ''2'':
([CategoryName] == ''Girls'' ? ''3'' :([CategoryName] == ''Groups'' ? ''4'' : ''5''))))
You need double quotes, not singles. If you let your mouse over over .... somewhere in that dialog box, you should get the error message
(DT_WSTR,100,1252)([CategoryName]=="16-24s" ? "1" :([CategoryName] =="Boys"? "2":
([CategoryName] == "Girls" ? "3" :([CategoryName] == "Groups" ? "4" : "5"))))
I have some values in my database which can be null if they have not already been entered.
But when I use Thymeleaf in my html, it gives an error when parsing null values.
Is there any way to handle this?
The shortest way is using '?' operator. If you have User entity with embedded Address entity in order to access fields of Address entity and print them if address is not null, otherwise here will be an empty column:
<td th:text="${user?.address?.city}"></td>
Sure there is. You can for example use the conditional expressions. For example:
<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty} : 'null value!'">someValue</span>
You can even omit the "else" expression:
<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty}">someValue</span>
You can also take a look at the Elvis operator to display default values like this:-
<span th:text="${someObject.someProperty} ?: 'default value'">someValue</span>
This can also be handled using the elvis operator ?: which will add a default value when the field is null:
<span th:text="${object.property} ?: 'default value'"></span>
You can use 'th:if' together with 'th:text'
<span th:if="${someObject.someProperty != null}" th:text="${someObject.someProperty}">someValue</span>
Also worth to look at documentation for #objects build-in helper:
https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#objects
There is useful: ${#objects.nullSafe(obj, default)}
You've done twice the checking when you create
${someObject.someProperty != null} ? ${someObject.someProperty}
You should do it clean and simple as below.
<td th:text="${someObject.someProperty} ? ${someObject.someProperty} : 'null value!'"></td>
<p data-th-text ="${#strings.defaultString(yourNullable,'defaultValueIfYourValueIsNull')}"></p>
you can use this solution it is working for me
<span th:text="${#objects.nullSafe(doctor?.cabinet?.name,'')}"></span>
I use
<div th:text ="${variable != null} ? (${variable != ''} ? ${variable} : 'empty string message') : 'null message' "></div>
The shortest way! it's working for me,
Where NA is my default value.
<td th:text="${ins.eValue!=null}? ${ins.eValue}:'NA'" />