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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

Requirements included after last <script> tag on template


Go to End


2 Posts   1497 Views

Avatar
Double-A-Ron

Community Member, 607 Posts

26 June 2013 at 4:02pm

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.

Avatar
(deleted)

Community Member, 473 Posts

26 June 2013 at 4:08pm

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.