We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6726
    • 7,075 Posts
    Ever since I discovered the "CSS as document" method, and solved problems with IE, I have totally dropped the "old" one (with static CSS files in the assets/templates/ folder). You had read about "server side css", well MODx is the only CMS who has it bundled grin !

    I had posted a while ago a tutorial in french about this method. After several request, here is a english translation :

    Pre-requisites :

    Using the CSS as MODx document method implies that you have friendly URLs enabled and that "Use friendly aliases" and "Use friendly alias path" is set to true ( See Administration > System Configuration > Friendly urls settings).

    1. In the manager, create a folder to store your stylesheets, not required but it’s cleaner. For the sake of this tutorial, this folder will be named "css".

    2. In this folder, create a new document (Content > New Document), and in "Uses template" select "(blank)" and go to Page settings > Content type, select "text/css"

    3. Insert your CSS there, or better yet use the power of chunk to build modular CSS, insert chunks containing your styles (for instance I often use a "layout" chunk, an "inline" styles chunk, etc...).

    4. Edit the document’s alias, appending .css at the end, it will override the default .html extension (not necessary, it will work without this).

    5. Edit your template :

    Usually, a template will include a call to the stylesheet with the "link method", e.g :

    <link rel="stylesheet" href="assets/templates/default/site.css" type="text/css" media="screen" />


    We are going to use the alternate @import rule (for some reason, the link method doesn’t work with IE when using the CSS as MODx document method). We’ll also use the stylesheet’s alias and NOT the [~xx~] syntax where xx is the stylesheet’s ID (again, for some reason doesn’t work with IE).

    <style type="text/css">
    @import url('css/my_style_sheet.css');                   /* or whatever css_folder_name/my_style_sheet.css */
    </style>


    Save your template.


    6. Edit your .htaccess : To ensure IE compatibility, you have to edit your .htaccess, uncommenting the following lines :

    BrowserMatch "MSIE" brokenvary=1
    BrowserMatch "Mozilla/4.[0-9]{2}" brokenvary=1
    BrowserMatch "Opera" !brokenvary
    SetEnvIf brokenvary 1 force-no-vary


    You’re set !
    You no longer have to use static stylesheets smiley

    Your CSS can include chunks and even snippets (wanna randomize a background image for instance, now it should be easy... but other things could be imagined... it’s up to coders to come up with snippet which can be used in the context of CSS). I didn’t test template variables, but since document are parsed no matter what their document type is, should work also -> Edit : TVs won’t work with CSS. See Susan’s explanation below

    Thanks to Susan Ottwell for the @import rule instead of good old [~id~] thing.
    Thanks to Eastbind for the .htaccess.

    Thanks to all who helped me solve the IE problems !
      .: COO - Commerce Guys - Community Driven Innovation :.


      MODx est l&#39;outil id
    • Fantastic! Great tutorial, David grin Thanks for sharing ... will definitely be useful.

        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 33337
        • 3,975 Posts
        Very nice detailed tutorial David!

        Keep up the good work.

        regards,

        zi
          Zaigham R - MODX Professional | Skype | Email | Twitter

          Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • I puzzled over Template Variables for a while, then the logic finally filtered through, and I realized that TVs can’t possibly work! TVs are connected to templates, right? Well, our css "document" has no template...

          So. Create an empty template just for our CSS file, and assign the TV to our new template as well as for the template of the document we want to display. Uh... the TV is edited for the document we want to display but it’s our CSS document that’s being parsed to generate the CSS file! All you could do is use a default value to have the same value for both documents, and you might as well use a chunk for that. So... by themselves, TVs can’t work in a CSS file.

          So I thought, you can have all the TVs you want for your document to be displayed, and use a snippet in your CSS to get the value of the TV for that document... er, how do you tell the snippet in the CSS file which document the CSS page is being generated for?

          We are dealing with two separate documents here; the CSS document is being requested and parsed just like any other HPPT page request.

          Bottom line, TVs won’t work in a CSS document.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 7974
            • 8 Posts
            Tell me if I am wrong but this seems like the long way around. I have used dynamic css for years. For something simple I just code php and css together and end the file in .php . Then link to it the same way you do a normal css file.

            This however breaks the idea of keeping logic seperate though so to keep them seperate I write a php script that will build the css file from as many css parts (modx would call these chunks) and link to the php script file.

            Now as for intergrating it with TV’s and all I’m not sure as I am new to MODx.

            I love the forums here, lots of good people and ideas,
            DragonWize
              • 7455
              • 2,204 Posts
              Yesterday my FF on mac one time did not load the css.. hope that this was beacause it was a monday. any one else had this?
                follow me on twitter: @dimmy01
                • 6726
                • 7,075 Posts
                Thanks all for the comments laugh

                @Susan : thanks for clearing up the TV thing !

                @Dimmy : I run FF 1.5.0.4 for Mac and having no trouble at all with the website using this technique... Also works with Safari.

                @all : if you find any glitch in this tuto please let me know !

                @DragonWise : I don’t think it’s a long way around. It might seem like it reading the tuto, but using this technique is quite straightforward for anyone familiar with MODx. I don’t see where it breaks the separation of content and presentation ?!?? Chunks containing css are separate from chunks containing html code...

                I don’t see how using php files to build modular css would be easier... ?
                  .: COO - Commerce Guys - Community Driven Innovation :.


                  MODx est l&#39;outil id
                  • 26439
                  • 104 Posts
                  I used to use a .php file for an .css file, I was playing around with browser detection and called the .css file with variable or two.

                  css.php?res=1024&browser=IE

                  Then have in the css.php file stuff like this ( amongst other things )

                  if ( res > 800 ){
                   " Big picture "
                  }
                  


                  I don’t anymore, but I was looking for the correct syntax for the header of a css / php hybrid, which is

                  header('content-type:text/css');
                  


                  ... incidently.

                  When I came across this, which may be of interest...

                  http://icant.co.uk/articles/cssconstants/



                    • 6726
                    • 7,075 Posts
                    This article reminds me of the famous one from Shaun Inman : http://www.shauninman.com/plete/2005/08/css-constants

                    Your example is exactly the kind of thing we can imagine doing with MODx ability to parse CSS, only we need a new breed of snippet to do just that tongue
                      .: COO - Commerce Guys - Community Driven Innovation :.


                      MODx est l&#39;outil id
                      • 7974
                      • 8 Posts
                      @scraf That is a great article. It descibes exactly what I do. I love how he slowly goes through all the possibilities and give them their due and coming to the best conclusion through logic.

                      @davidm I am sorry for the confusion. I was not implying that your method mixed logic. I was refering to this statement made by myself the line before:

                      For something simple I just code php and css together and end the file in .php .

                      And I was just refering to chunks as being the same as small parts of css in my example. Trying to relate what I did in terms of modx.

                      The point I was trying to make about being the long way around is that you have to do things like modify your htaccess file. The worst part about this is that you are modifying the file just to get it to work with a browser. I don’t like to use hacks like this unless I absolutely have to. With php there are no browser limitations, settings to set, or worries about what each host offers or client has enabled; it just works. The logic described in this article mention by scraf explains my thinking exactly.

                      http://icant.co.uk/articles/cssconstants/

                      Again sorry for the confusion,
                      DragonWize