Thomas Krantz
Aug 12, 2010
  3944
(1 votes)

Insert EPiServer page properties into CSS-files with EPiCssModifier

Have you ever wanted to replace hard wired image urls in your css files with images selected from EPiServer? That is, without using inline css in your templates.

Usually, your css may look something like:

image

You want to let the editors pick the image for the logo, right?

If you’re using PageTypeBuilder – which you should – then it’s only a matter of adding a property attribute to your page type class. Like so:

image

The CssFragment attribute in the property declaration means that you want to replace /ui/gfx/logo.png in all css-files with whatever value LogoImage contains.

What happens when you render the page is that the css links are modified slightly. This is your original css link:

image

Instead, it will render:

image

Note the appended query string to the css link.

Then a http handler will catch the request to the css-file and do a simple search and replace based on the query string parameters. The result will be a css that looks like this:

image

Prerequisites:

1. EPiServer CMS 6

2. PageTypeBuilder

To use:

1. Unzip, compile and drop the EPiCssModifier.dll in your /bin

2. Add this to the Web.config:

image

3. Voila! You’ve got yourself an inline-css free road to happiness.

Download source and binaries for EPiCssModifier

Aug 12, 2010

Comments

Ted
Ted Sep 21, 2010 10:33 AM

Great post! That's a pretty neat approach for certain theming scenarios!

Sep 21, 2010 10:33 AM

Simply brilliant. Just love it!
/ Martin Söderlund

Sep 21, 2010 10:33 AM

Really sweet, as you mentioned before today, this piece of code can be used to replace strings in JS-filed etc. aswell. WIN!
/ Robert Andersson

Sep 21, 2010 10:33 AM

Another option, to save the parsing and text replacement on each request would be to simply put your "dynamic part" of the css in the head of the master page. :-)

Thomas Krantz
Thomas Krantz Sep 21, 2010 10:33 AM

Steve: well that would be an option if you don't mind mixing in some css in your master page, but if your frontend devs only touch the css files then it's not an option. :)

Of course, there are some performance issues that need to be dealt with regarding the impact of html rewriting, parsing and text replacement etc.

Sep 21, 2010 10:33 AM

True, your solution is prettier, and easier on the front-end girls too :-) Making sure the css is cached on the client is even more important with this solution.

samuel.mitts@halvarsson.se
samuel.mitts@halvarsson.se Sep 21, 2010 10:33 AM

Thanks! Downloaded this today and using it now on a website... Works really nice! :)

David Gross
David Gross Sep 21, 2010 10:33 AM

Your my hero!

Anders Murel
Anders Murel Aug 29, 2011 09:18 AM

Morning Thomas!

The download URL is broken. Any chance you could fix it?

anders

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024