8

Our writers love to use the Twitter embed codes in Tridion (2011 SP1) to publish little embedded tweets. For example, they use the following type of code from Twitter's site:

<blockquote class="twitter-tweet"><p>Creating a great shopping experience. SDL  Fredhopper 7.5. Find out more: <a href="http://t.co/uhEiFZwqTE"  title="http://ow.ly/lhiL2">ow.ly/lhiL2</a> <a  href="https://twitter.com/search/%23ecommerce">#ecommerce</a></p>&mdash; SDL WCM Solutions  (@sdltridion) <a href="https://twitter.com/sdltridion/status/337878397172740097">May 24,  2013</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

To do this, they:

  1. Switch to the Source Tab of the RTF field

  2. Paste the code above

This works relatively well, EXCEPT when they later switch between Design and Source

  1. Source STARTS by showing the code fine when you copy and paste

  2. If you switch to Design, you will see the Twitter embed piece

  3. If you switch back to source, the code changes (I think Tridion is validating it), and it becomes this:

     <iframe id="twitter-widget-0" class="twitter-tweet twitter-tweet-rendered" scrolling="no" frameborder="0" allowtransparency="true" title="Embedded Tweet" width="500" height="186" name="twitter-widget-0"/> 
      <script async="" src="//platform.twitter.com/widgets.js" charset="utf-8" type="text/javascript">
      <![CDATA[]]>
      </script>
     <iframe id="rufous-sandbox" scrolling="no" frameborder="0" allowtransparency="true" name="rufous-sandbox"/>
    

The problem with this is that the content won't render properly anymore. It adds CDATA (which may or may not be the problem), and it also seems to execute some Twitter code that ends up with that iframe in there (see rufous-sandbox above)

What I'm wondering:

  1. Is there a way to strip out the CDATA tags?

  2. Is there a way to tell Tridion not to run that JavaScript code in there when it switches between design and source?

4 Answers 4

8

I don't believe that the SDL Tridion RTF field should be used to allow the pasting of scripts and other complex html items. It works, but as editors have the ability to change this in the WYSIWYG view, 99% of the time they end up removing, copying or breaking inserted functionality.

When I need to offer functionality to insert into rtf content, I like to built some user tags into the content to manage this. This is similar in behavior to wordpress shortcodes (http://codex.wordpress.org/Shortcode_API) - essentially the content editor puts in a simple tag such as [tweet id='23434234'] and a TBB will switch out the rest.

The advantages are:

  1. you can control what can be embedded and train users around these items
  2. you can store the embed code in a central location. Imagine 10,000 plus pages that need to change when twitter changes their api / url format (it could happen!)
  3. it's a lot cleaner in the WYSIWYG

If you're interested in the technical solution (code etc) let me know and I'll dig it out.

5

I was going to suggest an XSLT transformation, but there's nothing in the generated source that you could use to rebuild the original code. I don't think stripping out the CDATA will help; you'll just end up publishing an empty iframe as there is no script to publish the content.

Is there a valid reason for doing this in the rich text editor? If this is something editors are doing lots of, would it not be better to define this as a type of content and have them add it to pages with whatever template suits?

2

I suspect if you include the script tag in your template code and instruct your editors to just include the blockquote part of the embed code in the source tab. Your problem will be solved.

So include this with the other javascript includes in your master page template or page template:

<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

Then the editors just include this part:

<blockquote class="twitter-tweet"><p>Creating a great shopping experience. SDL  Fredhopper 7.5. Find out more: <a href="http://t.co/uhEiFZwqTE"  title="http://ow.ly/lhiL2">ow.ly/lhiL2</a> <a  href="https://twitter.com/search/%23ecommerce">#ecommerce</a></p>&mdash; SDL WCM Solutions  (@sdltridion) <a href="https://twitter.com/sdltridion/status/337878397172740097">May 24,  2013</a></blockquote>

That should prevent the script from executing when switching to the design view (because it isn't there) but it would render fine when the page/component was published and visible on the website.

This would also prevent multiple HTTP requests if more than one tweet were embedded in an RTF field.

I would consider John Winter's answer as a more robust solution over this though.

Hope that helps.

1

if you have bandwidth/time to work on Johnwinter's solution go for it. Else for that particular rich text field alter the XSLT as per your specific requirement. You can use altova /Visual studio to create & debug the XSLT.

1
  • 1
    I follow the same approach as John suggested. It takes very little time because it typically involves a simple TBB with String.Replace(definedToken, parameterSchema.ActualScriptCode), or ansimple regex. Whereas getting the XSLT just right in all the scenarios can be a big ordeal. Jun 2, 2013 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.