SOLVED

Re: Using velocity to truncate a URL after the ? (and including the ?)

Go to solution
Dan_Stevens_
Level 10 - Champion Alumni

Using velocity to truncate a URL after the ? (and including the ?)

I need the output of a velocity script to include just the URL (and non of the URL parameters that follow the ? (stripping out the ? as well).  Can you velocity gurus assist?  I'm thinking I need to use "TruncateAtWord".

2 ACCEPTED SOLUTIONS

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Using velocity to truncate a URL after the ? (and including the ?)

LinkTool is what you should use for URL manipulation (don’t try to parse URLs yourself).

#set( $url = $link.uri($lead.urlField) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
${url}

 

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Using velocity to truncate a URL after the ? (and including the ?)

Should do that check before parsing the URL.

#if( !$lead.lastFormURL.isEmpty() )
#set( $url = $link.uri($lead.lastFormURL) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
#set( $final = ${url} )
#else
#set( $final = "Not Available" ) 
#end 
${final}

View solution in original post

7 REPLIES 7
SanfordWhiteman
Level 10 - Community Moderator

Re: Using velocity to truncate a URL after the ? (and including the ?)

LinkTool is what you should use for URL manipulation (don’t try to parse URLs yourself).

#set( $url = $link.uri($lead.urlField) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
${url}

 

Dan_Stevens_
Level 10 - Champion Alumni

Re: Using velocity to truncate a URL after the ? (and including the ?)

SanfordWhiteman
Level 10 - Community Moderator

Re: Using velocity to truncate a URL after the ? (and including the ?)

Well, if you had a Person field called URL Field ($lead.urlField in my code) set to the first URL, the output would be the second URL. 😉

 

It works by

  • creating a Link object
  • setting the Link’s query string and hash to null — I assumed you wanted to strip the hash too, if it exists
  • then printing the Link value, i.e. the whole href
Dan_Stevens_
Level 10 - Champion Alumni

Re: Using velocity to truncate a URL after the ? (and including the ?)

The URL is stored in a custom field: "LastURL".

 

I still don't see where I would truncate the ? - and everything after it - using the code you provided.

SanfordWhiteman
Level 10 - Community Moderator

Re: Using velocity to truncate a URL after the ? (and including the ?)

? The code is truncating the final URL. That’s what happens when you null out the query string property before outputting the URL.

 

It’s like doing document.location.search = ""; in JavaScript.

Dan_Stevens_
Level 10 - Champion Alumni

Re: Using velocity to truncate a URL after the ? (and including the ?)

Now I see.  Thanks so much, Sandy - this worked perfectly!  I also included additional code to return "Not Available" if the last URL does not exists:

 

#set( $url = $link.uri($lead.lastFormURL) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )

#if( !$lead.lastFormURL.isEmpty() )
#set( $final = ${url} )
#else
#set( $final = "Not Available" ) 
#end 
${final}

 

 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Using velocity to truncate a URL after the ? (and including the ?)

Should do that check before parsing the URL.

#if( !$lead.lastFormURL.isEmpty() )
#set( $url = $link.uri($lead.lastFormURL) )
#set( $void = $url.setQuery($display.alt()) )
#set( $void = $url.setFragment($display.alt()) )
#set( $final = ${url} )
#else
#set( $final = "Not Available" ) 
#end 
${final}