Creating a tweet this link, HTML encoding

tdb's Avatar

tdb

20 Mar, 2015 12:43 PM

What I'd like to do is have a user be able to enter these two items into Data Definition fields:

Tweet text:
MSU geologist Masako Tominaga & her students map the ocean floor to study the earth’s polarity.

Trackable URL:
go.msu.edu/FLF5

Then be able to generate a "tweet this" link in Velocity that would result in a final URL like:

http://twitter.com/intent/tweet?text=MSU+geologist+Masako+Tominaga+%26+her+students+map+the+ocean+floor+to+study+the+earth%E2%80%99s+polarity.+go.msu.edu%2FLF5+%23SpartansWill

Any help would be appreciated, I figured that it might have been something that someone already has done.

  1. 1 Posted by Ryan Griffith on 20 Mar, 2015 06:44 PM

    Ryan Griffith's Avatar

    Hi,

    Once you set up the Data Definition fields, you can use the Velocity Escape Tool to encode the string and URL.

    Consider the following snippet to get you started:

    #*
    Assuming the following Data Definition XML:
    
    <system-data-structure>
      <group identifier="tweet" label="Tweet Link">
        <text identifier="text" label="Text"/>
        <text identifier="link" label="Link"/>
      </group>
    </system-data-structure>
    *#
    
    #set ( $text = $currentPage.getStructuredDataNode("tweet/text").textValue )
    #set ( $link = " ${currentPage.getStructuredDataNode('tweet/link').textValue}" )
    
    ## If the total tweet length is greater than 140 characters, truncate the text accordingly
    #set ( $fullTweet = "${text}${link}" )
    #if ($fullTweet.length() > 140)
        ## Find the maximum length for the text by subtracting the link length and 3 (to account for the ellipsis added during truncation).
        #set ( $truncateLength = $_MathTool.sub(140, $_MathTool.add($link.length(), 3)) )
        #set ( $text = $_DisplayTool.truncate($text, $truncateLength, "...") )
    #end
    
    http://twitter.com/intent/tweet?text=${_EscapeTool.url($text)}${_EscapeTool.url($link)}
    

    Note: this code will also account for cases where the entire tweet text is greater than 140 characters and will truncate the text before adding on the url.

    Please let me know if you have any questions.

    Thanks!

  2. Ryan Griffith closed this discussion on 08 Apr, 2015 02:33 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM