Compare string with velocity

27,216

Solution 1

Well I found the solution : I called twice to velocity : the first time without the player object and second one with it. So at the first call all directives concerning player could not be evaluated and returned null. Thank you all for your help

Solution 2

I would recommend to test this

#set($brandName = "NameExample")
#set($brandNameExample = "NameExample")
#if($brandName == $brandNameExample)
11111
#else
22222
#end

if it works then I would try to output $player, $player.brand, $player.brand.name you need to make sure that $player.brand.name - keeps correct value. - case sensivity? - sure that spell '$player.brand.name' correctly?

I think you simply has problem in object $player

Share:
27,216
mordekhai
Author by

mordekhai

Updated on May 28, 2021

Comments

  • mordekhai
    mordekhai about 3 years

    Considering the following piece of code in a velocity template:

    #set($brandName = $player.brand.name)
    #set($brandNameExample = "NameExample")
    
    #if($brandName == $brandNameExample)
        11111
    #else
        22222
    #end
    

    I always get 22222. Of course, player.brand.name = "NameExample".

    Can anybody explain me why and how to get it work please?

  • mordekhai
    mordekhai over 11 years
    I tried this and it works. But in my example it still does not. While debugging I saw that my player object contains a brand object that is lazy-loaded. But anyway when I check the parameters I give to velocity everything is here so I don't really understand.
  • mordekhai
    mordekhai over 11 years
    I forgot to say that from the beggining when I display $player.brand.name I get the correct value ! :) The problem seems to occur only when comparing.
  • Dmytro Pastovenskyi
    Dmytro Pastovenskyi over 11 years
    what if you try $brandName.equals($player.brand.name) ?
  • mordekhai
    mordekhai over 11 years
    I tried everything : $player.brand.name gives me the good value but when I compare I get false. Here is another test I made that failed : $player.getBrand().getName().equals("NameExample") #if($player.getBrand().getName().equals("NameExample")) 11111 #else 22222 #end I get : true 22222 ... I will try with the brand id instead of the name :)
  • mordekhai
    mordekhai over 11 years
    I think that your test worked because "NameExample" is the same string used for both variables brandName and brandNameExample so the address is the same that is why == gives true.
  • rinilnath
    rinilnath over 2 years
    Can we check equalsIgnoreCase in VTL ?