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

      • DougS's avatar
        DougS
        Khoros Oracle

        Velocity just exposes java objects and so the methods you can call on them are the public methods on that java object.

         

        The .get method they are using in https://velocity.apache.org/engine/1.5/user-guide.html#loops is an example when looping over a Hashtable (which has a  public get method). In that article they use this example:

        $allProducts.get($key)

         $allProducts in that article is a Hashtable, so it has a .get method.

         

        In your case, you use the .split method to split a string into a String array. Arrays don't have a .get method:

         #foreach ($part in $splitPostUrl)

        You don't need it though, because you have the array element in your loop (the $part variable).

         

        -Doug