With a bit of SCSS, ExpressionEngine and a color picker field type, we gave our client the ability to update CSS throughout the site.
Our client has a fairly normal request: give each of our departments a unique color palette, while keeping the design integrity in-tact - AND - if we add new departments in the future, we want to be able to add those, with their custom colors too. We explored a couple of options, including writing some CSS overrides inline and building a custom CSS snippet they could edit for each department. These options forced the client to manage a lot of code, and gave the potential for significant breakage in their design if they did it wrong.
What if we gave the client a few color picker fields to customize the palette, and the system rebuilt the CSS after the record was saved? That would be slick, safe, and immensely helpful to our client who only wants to make a few tweaks and not learn a whole new set of rules for how to manage CSS on their site.
What did we add:
- Use the pages module to generate pages of the site
- Add a color picker field type to the channel used by the page
- Create a custom module to update the CSS from a SCSS definition file (this module includes an SCSS php compiler from https://github.com/leafo/scssp...) when the Channel Entry Submission End hook is called.
- Load the custom CSS in the page template
You could use Structure, Native Layouts or the classic Templates approach, but whatever path you choose, you'll need to store the custom color data in a channel entry and listen for the field to be updated in order to regenerate the custom CSS.
There are a few out there that will work fine. We went with Color Picker Plus to have the flexibility to define a custom palette for the customer if they ever wanted that. At the time of this writing, it's only $14.50, if that's too rich for your bones, take a look at the Noah Kuhn's free Color Picker field type. In the end, you need to end up with a hex value in the field content.
Custom module to update the CSS from the SCSS definition file.
This is where the magic happens. Build a simple extension to listen for the "Channel Entry Submission End" developer hook. When the right entry is being saved, load the color from the entry, load the SCSS file, compile it using the https://github.com/leafo/scssp... library with the CSS destination... and clear your cache because the work is done. We included a couple of ExpressionEngine fields in the SCSS file, which are swapped out in the extension prior to compilation. If there is no color set in the channel entry, we fall back to the corporate colors.
Extension Code (listening for the "entry_submission_end" hook)
In the SCSS file, we have a few variables:
Update your paths, and you're all set to go! The client can update the CSS with whatever field content you select in your channel entry, giving them the freedom and the safety of not being able to easily break their design.
Happy Coding Friends!