Jump to:

3397 Posts in 978 Topics by 854 members

Template Questions

SilverStripe Forums » Template Questions » Requirements included after last <script> tag on template

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

Page: 1
Go to End
Author Topic: 461 Views
  • Double-A-Ron
    Avatar
    Community Member
    603 Posts

    Requirements included after last <script> tag on template Link to this post

    A colleague just bought me something I've never seen and I can't see mention of this in the docs. This was originally a question, but by the time I finished writing it I had found my answer in sapphire/core/Requirements.php. So I'll leave what wrote as something searchable.

    Take these snippets:

    Page.ss

    <div>
    <script src="/js/myScript.js"></script>
    <p>Stuff</p>
    </div>

    Page.php

    function pageAction() {
    Requirements::javascript('mysite/javascript/thirdparty/jquery-1.4.2.min.js');
    Requirements::javascript('mysite/javascript/thirdparty/jquery.plugin.js');

    .....
    }

    The html output of this is:

    <div>
    <script src="http://domain.com/mysite/javascript/thirdparty/jquery-1.4.2.min.js"></script>
    <script src="http://domain.com/mysite/javascript/thirdparty/jquery.plugin.js"></script>
    <script src="/js/myScript.js"></script>
    <p>Stuff</p>
    </div>

    The reason why he bought this over was because the first, litteral JS include was a secure cert service that feeds a badge/image on the page. It was being broken due to the other files being injected there. (Don't ask how)

    What was interesting was the solution:

    Page.ss

    <div>
    <script src="/js/myScript.js"></script>
    <p>Stuff</p>
    </div>
    <script></script>

    Which resulted in:

    <div>
    <script src="/js/myScript.js"></script>
    <p>Stuff</p>
    </div>
    <script src="http://domain.com/mysite/javascript/thirdparty/jquery-1.4.2.min.js"></script>
    <script src="http://domain.com/mysite/javascript/thirdparty/jquery.plugin.js"></script>
    <script></script>

    Looking at core, Silverstripe does a strripos() for any '<script' tags in the template and injects the requirements before this point.

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Requirements included after last <script> tag on template Link to this post

    And this has been fixed in 3.1 so JavaScript requirements are correctly inserted before the first <script> tag in the <body> if one exists.

    461 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.