Search code examples
phpiframesmarty

How to set HTML code in "srcdoc" attribute of iframe?


<div id="email_content">
<iframe srcdoc="{$email_content}"></iframe>
</div>

As shown in below picture I am using iframe inside div#email_content to show exact preview of email content. I am trying to use srcdoc attribute to load email content in the iframe. Here the email content might be a plain text or HTML content designed through CkEditor. I tried using escape, htmlentities, etc. But the srcdoc attribute breaks because the attribute value contains pure HTML code and quotes.

Any work-around will be accepted.

Thanks!

Note: I don't want to use src attribute here.

enter image description here


Solution

  • You need to use htmlentities method in php

    <?php
    $str = '<a href="https://www.tarunlalwani.com">Go to tarunlalwani.com</a>';
    echo htmlentities($str);
    ?>
    

    And then assign that to srcdoc. It works as shown in below JSFiddle

    https://jsfiddle.net/zpx8qw1b/1/