Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Css Template

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

Page: 1
Go to End
Author Topic: 1602 Views
  • Mo
    Avatar
    Community Member
    505 Posts

    Css Template Link to this post

    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

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Css Template Link to this post

    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');

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Css Template Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Css Template Link to this post

    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.

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Css Template Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Css Template Link to this post

    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.

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Css Template Link to this post

    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.

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