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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Ordering Requirements


Reply


8 Posts   2754 Views

Avatar
ChrisBryer

Community Member, 95 Posts

9 November 2009 at 6:12pm

Edited: 09/11/2009 6:13pm

hi everyone,
I am trying to order the sequence of javascripts (both external files with custom scripts) however my custom scripts seem to always get included after the external scripts.

this:

Requirements::javascript('themes/XYZ/javascript/jquery-1.3.2.min.js');
Requirements::javascript('themes/XYZ/javascript/jquery.jfeed.js');
Requirements::customScript(<<<JS
var categories = new Array();
...
JS
);
Requirements::javascript('themes/XYZ/javascript/homepageControls.js');

comes out like this:

<script src="javascript/jquery-1.3.2.min.js" type="text/javascript" />
<script src="javascript/jquery.jfeed.js" type="text/javascript" />
<script src="javascript/homepageControls.js" type="text/javascript" />
<script type="text/javascript"> (custom script)

any ideas on how to get the custom scripts before the external javascript?

thanks alot,
-Chris

Avatar
banal

Community Member, 901 Posts

9 November 2009 at 10:51pm

Hi

The easiest way to do this is to put the includes directly in the template, without using Requirements.

Avatar
ChrisBryer

Community Member, 95 Posts

10 November 2009 at 4:28am

i've never seen someone put a custom script into a template, and havent really seen any documentation on the <% require %> control block. are you saying to not use the <% require %> control block and the requirements class in the controller and just use normal script tags that point to a source, or just not use the requirements class but use the <% require %> tag. I'd love to see an example if you have one.

thanks again,
-Chris

Avatar
yurigoul

Community Member, 202 Posts

10 November 2009 at 5:53am

Use $ThemeDir as in:

<script type="text/javascript" src="$ThemeDir/js/jquery.js"></script>

And put your js files in the folder js in your themedir. Same goes for other resources.

Avatar
ChrisBryer

Community Member, 95 Posts

10 November 2009 at 6:17am

sorry, perhaps my example was a little too undescriptive.

I am dynamically creating a javascript through php that gets inserted into the page template. the webhost's server is pretty poor performance and i need everything statically published, so rather than ajax calls, i do everything clientside with javascript and jquery and i push dataobjects into a javascript array. this javascript array needs to be written into the page before jquery and other jquery calls that manipulate the homepage using the javascript array.

so long story short, i can insert normal js files into the page template, but the dynamically created javascript needs to be inserted before other external scripts.

here is a little more code that roughly shows what the custom script is doing

Requirements::javascript('themes/XYZ/javascript/jquery-1.3.2.min.js');
Requirements::javascript('themes/XYZ/javascript/jquery.jfeed.js');

$dos = DataObject::get('Category', "`ParentID` =".{$this->ID});
$arrayVars = '';
foreach($dos as $do){
$arrayVars .= $do->Title .',';
}
Requirements::customScript(<<<JS
var categories = [$arrayVars];
...
JS
);
Requirements::javascript('themes/XYZ/javascript/homepageControls.js'); // THIS USES THE JAVASCRIPT ARRAY WRITTEN BY THE REQUIREMENTS::CUSTOMSCRIPT AND NEEDS TO COME AFTER THAT CUSTOM SCRIPT

i am getting by for now by waiting for window.load instead of document.ready, but i hate to depend on that.

Avatar
banal

Community Member, 901 Posts

10 November 2009 at 10:36am

Hi chris

Sadly, the requirements don't allow you to set a inclusion order. Same goes for the <% require %> control.
The only thing I can think of is the following:
Create a method named "CustomScript" in your Page_Controller and return the custom JavaScript there (simply return a string).
Then include your static scripts directly in the template, like this:

<script src="javascript/jquery-1.3.2.min.js" type="text/javascript" />
<script src="javascript/jquery.jfeed.js" type="text/javascript" />
$CustomScript
<script src="javascript/homepageControls.js" type="text/javascript" />

The $CustomScript variable will then be replaced by the return value of your CustomScript method.
As you can see, I fully skipped "Requirements" as it won't help to solve your problem.

Avatar
ChrisBryer

Community Member, 95 Posts

10 November 2009 at 10:39am

that makes sense. thanks for looking at this.

-Chris

Avatar
monkeyben

Community Member, 25 Posts

17 June 2011 at 10:57am

The way I have got around the problem of SilverStripe putting custom javascript above the SilverStripe built-in Javascript is to put the SS script in the head tags: -

Put this in your _config.php file: -

Requirements::set_write_js_to_body(false);

and then put your custom javascript tags just before the end body tag in your templates. Not perfect, but it works.