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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

require plain CDATA js-code trough function init()


Go to End


2 Posts   1908 Views

Avatar
theAlien

Community Member, 131 Posts

30 September 2009 at 1:34pm

Edited: 30/09/2009 1:35pm

Hi,

I would like to incorporate some plain CDATA js-code (code that's not in a .js-file) through the function init().
My js-code uses some data from a database-field, and if I put this variable in a .js-file it breaks.

So I ended up stuffing the plain js-code in an include-template, but that of course is a bad hack.

After a while I found out that the plain CDATA js in the default-template is inserted by the validator.php file on line 161:

function includeJavascriptValidation() {
...
$js = <<<JS
/* javascript here */
JS;
...
}

But inserting just a $js = <<<JS ... JS; in the init-method is too easy to be true

Does someone know how I could require some plain CDATA js-code?
Or maybe someone knows how to get the data in a databasefield into a required js-file?

Both ways would be fine.
NOTE: I'm quite a js-noob :-(

Avatar
Willr

Forum Moderator, 5523 Posts

30 September 2009 at 3:36pm

You have to use Requirements::customScript()

Requirements::customScript(<<<JS

// your JS here

JS
);