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.

Template Questions /

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

including CSS in my module


Go to End


3 Posts   2131 Views

Avatar
ismooth

Community Member, 25 Posts

22 September 2012 at 1:14am

Edited: 22/09/2012 1:14am

I've made my own module, and now I want to add some css for better data presentation. I've got following file structure:

- sigurnijiinternet
-- css
--- siginternet.css
-- templates
--- RedButtonPage.ss
-- code
--- RedButtonPage.php

I've tried including

<% require css(sigurnijiinternet/css/siginternet.css) %>
into template file. Also with calling themedCSS(siginternet.css) function. I've tried calling Requirements class' css and themedCSS functions from init function of RedButtonPage_controller within RedButtonPage.php file:

        public function init() {
            parent::init();
            Requirements::themedCSS("siginternet");
            // Requirements::css("/www/sigurnijiinternet/css/siginternet.css");
            // parent::init();
        }

Notice the variation on order of calling these functions. Furthermore, I've also tried changing user permissions on all mentioned files, but I never get any css included into the resulting html.

Any hints?

Avatar
martimiz

Forum Moderator, 1391 Posts

22 September 2012 at 4:29am

Edited: 22/09/2012 4:30am

You probably already found the following documentation for SS2.4 and SS3:
- http://doc.silverstripe.org/framework/en/2.4/reference/requirements
- http://doc.silverstripe.org/framework/en/reference/requirements

<% require css(sigurnijiinternet/css/siginternet.css) %>

Should work... You probaly also know to ?flush=1 if you change a template...

Requirements::css("sigurnijiinternet/css/siginternet.css"); 

Should work from your pagecontroller init() like you do (but not with the www though, but up from the root of your website)

ThemedCSS now, that will only work if you place your css file in your theme...

Avatar
ismooth

Community Member, 25 Posts

25 September 2012 at 10:33pm

martimiz,

Yes, I've checked the documentation, and also tried flushing. I also tried various paths (absolute, relative...) to the css file. What I noticed is that if I don't Include all html code in template file (html, head, body tag....) then css doesn't get included. When I struct template file good, then it gets included - this is okay.

But, I also noticed that some standard css files get included, like default theme's layout.css, js langugage file and so on. I've removed them with Requirements::clear. I guess that's why it's discouraged to include css, js and other files in controller's::init function.

P.S. Maybe a notice could be added in documentation that full html code must be put in template file for template requirements to work?

Cheers