Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Custom HTML Code / Javascript inserted via CMS

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2684 Views
  • CHD
    Avatar
    Community Member
    218 Posts

    Custom HTML Code / Javascript inserted via CMS Link to this post

    One of our clients has lots of javascript code snippets that they would like to include in their sites, entering them via the text editor in the CMS will obviously just strip out any HTML & JS tags, and saving them as plain tetx gets them into the DB fine, but of course SS escapes it when pulling it through into the template.

    So, it goes in like this:

    <script type="text/javascript" src="FOO"></script>

    and comes out like this:

    &lt;script type="text/javascript" src="FOO"&gt;&lt;/script&gt;<br>

    we tried creating a control function in the template with just the JS URL as the dynamic content which is inserted via the CMS, but unfortunately not all of their code snippets will work that way, some have images etc and forms.

    can anybody help at all with this? I'm sure there's a way I can get the data directly from the DB into the template without escaping the tags and coding.

    thanks in advance.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Custom HTML Code / Javascript inserted via CMS Link to this post

    You can use HTMLText for unescaped HTML content.

    http://doc.silverstripe.org/sapphire/en/topics/data-types

  • CHD
    Avatar
    Community Member
    218 Posts

    Re: Custom HTML Code / Javascript inserted via CMS Link to this post

    Thanks for the reply, I tried HTMLText first of all, but as soon as I paste in the code it adds lots of CD DATA?

    Here's the input:

    <script type="text/javascript" src="foo.php"></script>
    <a href="foo.com"><img src="foo.gif" width="135" height="103" alt="" border="0"></a>
    <script language="JavaScript" type="text/javascript">
    document.write('<form name="jpform12"><input name="progressive12" id="progressive12" readonly style="background-color: transparent; border-bottom-color: #99ffff; border-bottom-width: 0px; border-left-color: #99ffff; border-left-width: 0px; border-right-color: #99ffff; border-right-width: 0px; border-top-width: 0px; color: #000000; font-family: verdana; font-size: 12px; font-weight: bold; height: 15px; width: 115px; text-align: center;"><\/form>');
    ScrollProgressiveCounters(12);
    </script>

    Here's what happens when I save the page:

    <script type="text/javascript" src="foo.php"/><a href="foo.com"><img src="foo.gif" width="135" height="103" alt="" border="0"/></a>
    <script language="JavaScript" type="text/javascript"><![CDATA[<![CDATA[
    document.write('<form name="jpform12"><input name="progressive12" id="progressive12" readonly style="background-color: transparent; border-bottom-color: #99ffff; border-bottom-width: 0px; border-left-color: #99ffff; border-left-width: 0px; border-right-color: #99ffff; border-right-width: 0px; border-top-width: 0px; color: #000000; font-family: verdana; font-size: 12px; font-weight: bold; height: 15px; width: 115px; text-align: center;"><\/form>');
    ScrollProgressiveCounters(12);
    ]]]]><![CDATA[>]]></script>

    If I paste the code straight into the template, it works fine, as soon as I add it via the CMS nothing happens.
    Is it the coding itself that is the problem?

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Custom HTML Code / Javascript inserted via CMS Link to this post

    Yeah, I see the problem. But I have no solution so far.

    You can find the problem in SiteTree->syncLinkTracking(). :ugly:
    It checks all the HTMLText field types of the page class and runs them through HTMLEditorField->saveInto() -- which is exactly what we want to avoid.

    http://www.php.net/manual/en/class.domdocument.php

  • CHD
    Avatar
    Community Member
    218 Posts

    Re: Custom HTML Code / Javascript inserted via CMS Link to this post

    Thanks for getting back to me, yeah I see that too.
    There must be a way to disable this?

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Custom HTML Code / Javascript inserted via CMS Link to this post

    Well, you could go back to the Text fieldtype and create a wrapper method with a different name than the actual field.

    public static $db = array(
       'HTMLTextTest' => 'Text'
    );
    function getHTMLTextTest() {
       return $this->getField('HTMLTextTest');
    }

  • CHD
    Avatar
    Community Member
    218 Posts

    Re: Custom HTML Code / Javascript inserted via CMS Link to this post

    nope, that threw up an error.

    also, why would that be any different from just calling the field with $FieldName in the template?

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Custom HTML Code / Javascript inserted via CMS Link to this post

    Works for me. Did you flush your database? You have to call $getHTMLTextTest instead of $HTMLTextTest in your templates too.

    also, why would that be any different from just calling the field with $FieldName in the template?

    Because this is where the Convert::raw2xml() in your original posting comes from.

    2684 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.