Using velocity script and custom object I'm trying to script an IF and Else if statement with multiple conditions using (&&) and (!=). Here is a sample of the code.
I'm getting the content to render correctly for when it meets one of the condition (when variable.variableName == A OR == B) but I can't get it to render right for when variable.variableName equals to both A AND B. So pretty much the last condition is not working and I can't get it to show both A and B contents. Any ideas? Thanks!
#foreach($variable in $variable_cList)
#if(${variable.variableName} == "A" && ${variable.variableName} != "B")
#set($matched = ${variable.variableID})
Show content A
#elseif(${variable.variableName} != "A" && ${variable.variableName} == "B")
#set($matched = ${variable.variableID})
Show content B
#elseif(${variable.variableName} == "A" && ${variable.variableName} == "B")
#set($matched = ${variable.variableID})
Show A and B contents
#else
#end
#end
A variable can never be equal to two different values, so the third statement can never be true. Do you have a real example of the values you're trying to evaluate here?
Thanks Kenny. Good to know that a variable cannot equal to two different values, that could explain why it didn't work. Then how can I code up something for when I want something to render when both conditions are true?
I'm pulling from a custom object data call "competency".
Values are competency values such as : Cloud Platform, SMCS, etc.
So I want a block of content to render if someone has a competency values of both "Cloud Platform" and "SMCS".
I guess you want to check if the field CONTAINS A, B etc? Not Equals A, B, etc. Right?
#set ($string = ${competency.field})
#set ($RegEx = "RegularExpressionHere")
#if($string.contains("value"))
foo
#elseif($string.matches($RegEx))
bar
...
Both, I'm checking to see if it has any of the values (A, B or C) and if it has two of the values (== A and B). In my code above I'm able to check to see if the field contains A, B, or C but my issue is not being able to code for when I need it to meet both conditions (like A and B ).
For example:
If field is A or B or C = get story content 1
If field is A and B = get story content 2
If field is B and C = get story content 3.
#foreach($variable in $variable_cList)
#if(${variable.variableName1} == "A" && ${variable.variableName2} != "B")
#set($matched = ${variable.variableID})
<p>Your HTML - Show content A</p>
#elseif(${variable.variableName1} != "A" && ${variable.variableName2} == "B")
#set($matched = ${variable.variableID})
<p>Your HTML Show content B</p>
#else(${variable.variableName1} == "A" && ${variable.variableName2} == "B")
#set($matched = ${variable.variableID})
<p>Your HTML Show A and B contents</p>
#else
#end
#end
--
I'm not an expert (far from it) so probably you'll get a better answer elsewhere. But my understanding is that "else" refers to the first #if and #elseif being unmatched. Otherwise I think you just make everything an #if.
ok. so you loop through a list, right?
then I guess you need to add a counter or false/true of some sort.
hasA = 0
hasB = 0
hasC = 0
for each A
hasA = hasA +1
for each B
hasB = hasB +1
for each C
hasC = hasC +1
=> in the end check for the variations and define the content. e.g. hasA <> 0 && hasB = 0 && hasC =0 ...
Yes it does loop through a list. Cool, I'll give it a try! Thank you!