cssmodxmodx-revolutionmodx-templates

Can I manage my CSS as ModX Resources?


I've been working with the ModX Revolution manager, and was wondering if I can turn my CSS into ModX Resources rather than files. I'd also like to utilize Templates and their variables. Is that possible? What are the drawbacks of doing so? Are there any advantages (aside from the ones I think below)?

The reason I would prefer this is that I use several CSS pages with the @import statement to object orient my CSS a little bit better. If I could do this directly in ModX, it would save me uploads and syncing.


Solution

  • In previous versions of ModX (Evo and Revo), you used to have to go through tricks. Now with 2.2.2pl, it is possible with very few tricks. The information to do this is sparse and inaccurate any more. Here's how you do it:


    Create Your Template

    1. Create a new Template. I named mine CSS Stylesheet. (Simple as that)

    2. For the content, simply put [[*content]].

    Create Your CSS Page

    1. Create a new resource. Name it whatever you would like.

    2. Add your alias. Make sure you do not add the .css at the end. ModX should do this for you.

    3. Now, make sure your new page is published. You may also want to hide from menus.

    4. Add your CSS code. No funny tricks... Just copy and paste it like you normally would.

    Test the Stylesheet

    Simply navigate directly to your new page as if it were an html document. Don't forget the .css instead of .html. If you see the CSS code, then you've succeeded.

    Add Your CSS to the Templates

    This one is the tricky part. You won't be able to use the <link rel=></link>.

    1. Simply go to your <head> element. Add the following code:

      <style type="text/css"> @import url(""); </style>

    2. Test the

    3. Inside the url("");, just place the url to your new resource.

    Advantages

    Aside from the saving the uploading and downloading, you can now edit your CSS using any of the ModX tools. Additionally there are a few other perks:

    Drawbacks

    There's always trade-offs, and with this technique it is no different. A lot depends on how you have things set up for your site.


    Conclusion

    The whole process takes about 15 minutes. And ultimately, it's even faster to revert back if it doesn't work for you (just don't delete the raw files until you are sure). The added functionality is worth it to me.