Q:

Velocity parse error on a null value

I have a problem in my velocity template.
I have to show the image url for some products.
I set a variable in a right way. In some cases anyway I don't have this image, so I have to hide the image and put a blank space in the template.
I write the set variable in this way:
#set ($variantUrl = ${#if(!$product.getOrderFormImage().getUrl()) $!product.getOrderFormImage().getUrl() #else $product.getOrderFormImage().getUrl() #end} )

but I obtain a parse error:

Caused by: org.apache.velocity.exception.ParseErrorException:
  Encountered "(" at
  de.hybris.platform.commons.renderer.impl.VelocityTemplateRe
  nderer[line 403, column 103] Was expecting:
       ...

I don't see any error in this line. What's the problem?

A:

I'm not sure you can nest #if #else within #set - at least there's no mention of it in the Velocity user guide. 
When I use your original expression I get the following error:
org.apache.velocity.exception.ParseErrorException: Encountered "#if" at ....
Was expecting:
<IDENTIFIER> ...

If I rewrite to make the #if #else to be the top-level statement such as: 
#if( !$product.getOrderFormImage().getUrl() )
    #set($variantUrl = $!product.getOrderFormImage().getUrl() )
#else
    #set($variantUrl = $product.getOrderFormImage().getUrl() )
#end

this seems to do what you want, or at least it compiles and doesn't error!  It's also a lot easier to read and understand.
Personally, I'd go one step further for the purpose of readability and use Velocity's shorthand notation for references:
#if( !$product.orderFormImage.url )
    #set($variantUrl = $!product.orderFormImage.url )
#else
    #set($variantUrl = $product.orderFormImage.url )
#end

