velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: problem parsing references in Velocity
Date Thu, 12 Jun 2008 01:49:09 GMT
Andy,

andyandy wrote:
> I need to send some data to an external service that interacts with my
> application. It's just a group of ids and values that the service needs. The
> real code that I have is:
> 
> #set
> ($query="id_maq=escape($!{idMaq})&referer=escape($!{referer})&urlv=escape($!{urlv})&urlf=escape($!{idMaq}$!{url})&serv=escape($!{serv})")
> 
> The problem is that when any of the variables is null it tries to
> "escape(null)", which I don't want. I only want to escape the values that I
> do have.

You can do this in many ways.

1. Use iterative conditionals

   #if($idMaq)
     #set($query = "$query&amp;id_maq=${idMaq})
   #end
   #if($referer)
     #set($query = "${query}&amp;referer=${referer})
   #end
   .
   .
   .

2. Write a macro to do the conditionals for you:

   #macro(possiblyEscape $paramName $paramValue)
   #if($paramValue)&amp;${paramName}=${paramValue}
   #end

   (note that you might want to put this all on one line to avoid
   excess whitespace)

   ($query = "#possiblyEscape('id_maq', 
$idMaq)#possiblyEscape('referer', $referer) ... ")

I'm sure there are other options.

Also note that you might want to run your parameter names and values 
through URLEncoder.encode. Consider using LinkTool from Velocity Tools, 
which handles lots of stuff for you like this.

-chris


Mime
View raw message