Forum Discussion

ttadej's avatar
ttadej
Advisor
8 years ago

Issues using .split() and .get(index) in Velocity email templates

We're add UTM tags to links in our email templates and need to split the post URL into 2 parts so it can be rendered as follows:

Example: ${urlPath}?utm_tags#${urlHash}

I've been unable to successfully use the Velocity .split() and .get(index) functions. Anyone else tried this or have another solution?

Separately, any idea which Velocity version is running on Lithium?

Thanks

  • I believe you should be able to do something like this:

    #set ($splitPath = urlPath.split(","))
    #foreach ($path in $splitPath)
      ${path}
    #end

     -Doug

  • DougS's avatar
    DougS
    Khoros Oracle

    I believe you should be able to do something like this:

    #set ($splitPath = urlPath.split(","))
    #foreach ($path in $splitPath)
      ${path}
    #end

     -Doug

    • ttadej's avatar
      ttadej
      Advisor

      Thanks DougS

       

      We ended up doing this, which seems excessive but worked:

              #set ($postUrl = $notification.message.webUi.url)
              #if ($postUrl.contains('#'))
                #set ($splitPostUrl = $postUrl.split('#'))
                #foreach ($part in $splitPostUrl)
                  #if ($velocityCount == 1)
                    #set ($urlPath = $part)
                  #elseif($velocityCount == 2)
                    #set ($urlHash = $part)
                  #end
                #end
      
                <a href="$!{urlPath}?${utmTag}#$!{urlHash}">
              #else
                <a href="${postUrl}?${utmTag}">
              #end

      This isn't much documentation here: https://velocity.apache.org/engine/1.5/user-guide.html#loops

      Unfortunately $arr.get(index) does not seem to work even though Lithium is using Velocity v1.5

      • Inactive User's avatar
        Inactive User

        ttadej Does your code work? I have tried it in one of our emails and it isn't working as I would expect. Did you change anything in it?