Using velocity to truncate a URL after the ? (and including the ?) | Community
Skip to main content
Dan_Stevens_
New Participant
May 13, 2024
Solved

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

  • May 13, 2024
  • 1 reply
  • 3228 views

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".

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

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}

 

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
May 13, 2024

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}

 

SanfordWhiteman
New Participant
May 14, 2024

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