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.

General Questions /

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

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

Adding css / javascript requirements in controller


Go to End


4 Posts   1381 Views

Avatar
Faloude

Community Member, 55 Posts

6 March 2015 at 11:24am

This works just fine, but I'm just trying to understand the logic here..

How is it possible that when you code your javascript / css file requirements in Page.php as below, it shows up in the right place in your page (within the <head> area)? You're defining the files in the Page.php, but you're not really calling them back in Page.ss, yet they magically show up in the right place.

public function init() {
		parent::init();
		// You can include any CSS or JS required by your project here.
		// See: http://doc.silverstripe.org/framework/en/reference/requirements
		Requirements::css($this->ThemeDir()."/css/style.css");
	}

Avatar
zigonick

Community Member, 6 Posts

6 March 2015 at 11:33am

From what I can tell it adds all the css calls right before it sees </head>. and for the Javascript requirements it puts its right after </body> (Forgot to put </body> recently and javascript wasn't getting loaded in but once i put that in it added the <scripts>

Avatar
Pyromanik

Community Member, 419 Posts

6 March 2015 at 11:40am

Yep, simple find/replace on </head> (css) and/or </body> (js).

JS can be forced to the head with a configuration setting. Not recommended.
JS will also be inserted before any already (hard coded) <script> tag in the <body> in case there are dependencies needed from your Requirements class calls.
This can be a trap when you have for instance: <!--<script> as suddenly all your JS tags via Requirements is now commented.

Avatar
Faloude

Community Member, 55 Posts

11 March 2015 at 3:30am

Thanks, now it's clear to me :-)