Question

I want to include a Facebook post using a template from the Play framework. The Problem is that, even though @Html should not escape HTML entities, they get escaped. As a result " turns into " and the script is not evaluated.

Embed-Code

post = "<div id="fb-root"></div> 
<script>
  (function(d, s, id) { 
    var js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) return; 
    js = d.createElement(s); 
    js.id = id; 
    js.src = "//connect.facebook.net/de_DE/all.js#xfbml=1"; 
    fjs.parentNode.insertBefore(js, fjs); 
   }(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-post" data-href="https://www.facebook.com/FacebookDevelopers/posts/10151945402933553" data-width="466">
  <div class="fb-xfbml-parse-ignore">
    <a href="https://www.facebook.com/FacebookDevelopers/posts/10151945402933553">Beitrag</a> von <a href="https://www.facebook.com/FacebookDevelopers">Facebook Developers</a>.
  </div>
</div>"`

Template @Html(post)

Output

<div id="fb-root"></div> 
<script>
  (function(d, s, id) { 
      var js, fjs = d.getElementsByTagName(s)[0]; 
      if (d.getElementById(id)) return; 
      js = d.createElement(s); 
      js.id = id; 
      js.src = &quot;//connect.facebook.net/de_DE/all.js#xfbml=1&quot;;
      fjs.parentNode.insertBefore(js, fjs);
    }(document, &apos;script&apos;, &apos;facebook-jssdk&apos;));
</script>
<div class="fb-post"; data-href="https://www.facebook.com/FacebookDevelopers/posts/10151945402933553" data-width="466">
   <div class="fb-xfbml-parse-ignore">
     <a href="https://www.facebook.com/FacebookDevelopers/posts/10151945402933553">Beitrag</a> von <a href="https://www.facebook.com/FacebookDevelopers">Facebook Developers</a>.
   </div>
 </div>

I'm really stuck, because @Html() is not doing what it's supposed to do according to the doc.

Was it helpful?

Solution

It works exactly as the docs says, try this out for example: Template:

@()
@Html("<div>&\"</div>")

Will give you this output:

<div>&"</div>

So there must be something else going on in your app that html-escapes the string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top