In a previous post, Create a User Config Page in MODx, I referenced how you could create a dynamic CSS document in MODx. While on most of my development projects I haven’t found this to be a necessary step, there are a few occasions in which this capability can provide a lot of flexibility.
Create Your Document
In order to create a CSS document in MODx you will create a new Resource (page). First, Title your document something like ‘CSS’, enter the URL alias as ‘style.css’, select (blank) beside Uses Template and uncheck the Show in menu checkbox. Next, under the Settings tab, uncheck Searchable to keep the page from showing up in a site search and then uncheck Cacheable if it is appropriate for your usage.
This should generate a style.css document that you can reference in your HTML head like:
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
Usages
In my example post, Create a User Config Page in MODx, I was giving the end user the ability to control Template Variables (TV) that were being called into the CSS document with a GetField snippet:
body{background:[[GetField? &docid=`1` &field=`backgroundColor`]];}
This can be applied to any CSS attribute like font color, link color, background color/images, etc…
It is also conceivable that you could use the PHx snippet to display different attributes depending on which template ID is in play or to display a default attribute unless a TV field is populated, allowing the TV to override the default attribute:
[+phx:if=`[[GetField? &docid=`1` &field=`backgroundColor`]]`:is=``:then=`#8fc644`:else=`[[GetField? &docid=`1` &field=`backgroundColor`]]`+]
I have used the above example to display a default background image in a site header. My designer then wanted a few, select pages to display different header images. I then created a template variable with an ‘Image’ Input Type and if these TVs were populated, the header displayed a different background image only on that page.
These are just a couple of examples of how this technique can be adapted for your site(s). What’s really exciting for me is that CSS is my “first love” and how broadly this could applied to site developments.




Pingback: My MODx Template Strategy | Greg Smart
Pingback: Creating a User Config Page in Revo | Greg Smart