Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Custom HTML Code / Javascript inserted via CMS


Go to End
Reply


10 Posts   2759 Views

Avatar
CHD

Community Member, 218 Posts

17 November 2011 at 6:38am

Edited: 17/11/2011 6:38am

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.

Avatar
Devlin

Community Member, 215 Posts

18 November 2011 at 12:34am

You can use HTMLText for unescaped HTML content.

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

Avatar
CHD

Community Member, 218 Posts

18 November 2011 at 12:59am

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?

Avatar
Devlin

Community Member, 215 Posts

18 November 2011 at 3:25am

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

Avatar
CHD

Community Member, 218 Posts

18 November 2011 at 3:29am

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

Avatar
Devlin

Community Member, 215 Posts

18 November 2011 at 4:23am

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');
}

Avatar
CHD

Community Member, 218 Posts

18 November 2011 at 4:40am

nope, that threw up an error.

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

Avatar
Devlin

Community Member, 215 Posts

18 November 2011 at 4:49am

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.

Go to Top