Jump to:

23361 Posts in 18124 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Ordering Requirements

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

Page: 1
Go to End
Author Topic: 2589 Views
  • ChrisBryer
    Avatar
    Community Member
    95 Posts

    Ordering Requirements Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Ordering Requirements Link to this post

    Hi

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

  • ChrisBryer
    Avatar
    Community Member
    95 Posts

    Re: Ordering Requirements Link to this post

    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

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: Ordering Requirements Link to this post

    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.

  • ChrisBryer
    Avatar
    Community Member
    95 Posts

    Re: Ordering Requirements Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Ordering Requirements Link to this post

    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.

  • ChrisBryer
    Avatar
    Community Member
    95 Posts

    Re: Ordering Requirements Link to this post

    that makes sense. thanks for looking at this.

    -Chris

  • MonkeyBen
    Avatar
    Community Member
    25 Posts

    Re: Ordering Requirements Link to this post

    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.

    2589 Views
Page: 1
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.