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

Css Template


Go to End


7 Posts   2416 Views

Avatar
Mo

Community Member, 541 Posts

5 June 2009 at 9:57pm

Hi all,

Is there a way of using a css template, similar to the Requirements::javascriptTemplate method?

I have checked the API documentation, and I cant see a cssTemplate method, or anything similar.

Any Ideas?

Mo

Avatar
Nivanka

Community Member, 400 Posts

6 June 2009 at 3:04pm

Do you need to know about importing a CSS file to the webpage?

if that is the question you can do it with Requirements::css('filename');

Avatar
Mo

Community Member, 541 Posts

11 June 2009 at 3:40am

No, I was looking at replacing a variable in my css with something generated by the CMS.

In this case, the background image for the page is chosen in the cms, and then needs to be added as a background image to the css for the page.

There are also options for adding padding to the page and positioning the image, so that its alignment can be fine tuned.

Cheers,

Mo

Avatar
bummzack

Community Member, 904 Posts

11 June 2009 at 4:09am

Hi Mo

I usually do such things like this:
Create the stylesheet the usual way, maybe even with default values for the customizable Parameters.
In the template, I add a custom <style> block where I add the custom style values. If you got a BackgroundColor member for your Page class, you could add the following to your Page template:

<style type="text/css">
body { background-color:$BackgroundColor; }
</style>

Or, for increased flexibility, use the Requirements::customCSS method which does basically the same.

I think this procedure is way faster in terms of processing time and page load (since the base css file remains static and can be cached).
This site: http://jugendfachstelle.ch/ is a good real-life example for this. The Administrators of the site can freely choose the background color and the color of the stickies.

Avatar
Mo

Community Member, 541 Posts

11 June 2009 at 5:08am

I am using Requirements::customCSS currently. which works fine. But I like my code to be as separate as possible, so that my CSS, JavaScript and PHP can be in separate places.

Probably a bit O.C.D, but that way I don't have to start searching through PHP classes looking for CSS that is overwriting something else :).

Mo

Avatar
bummzack

Community Member, 904 Posts

11 June 2009 at 6:10am

You've got a point, that's why I'm putting the code directly into my templates instead of using Requirements::customCSS. Not optimal too, but the template belongs to the view, and so does the CSS.

If you would use something like a "CSS Template" as described in your first post, you would still mix CSS with PHP, just in a more subtle way. In that case, figuring out where CSS definitions come from will be even harder than the other way round.

Avatar
Mo

Community Member, 541 Posts

11 June 2009 at 7:07am

That makes sense, I guess personally I have a naming convention that any css/js files specific to a class contain the class name in their filename (or as there filename). So I can fairly easily pick out class specific files.

That process does end up with a lot of small files, if you aren't careful though! :S

I think I will start putting CSS like this into my templates though, I think that follows the whole MVC thing a bit more closely.