SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Variables in CSS (very easy to implement in MVC)

    The question of using variables in CSS seems to come up from time to time so I decided to do a bit of experimenting and ASP.NET MVC just makes it too easy to implement.

    Basically you just point a <link> to your controller, add a MapRoute to get it there, define a class, add some definitions, add a strongly typed view and change the contentType of the view.

    That's it. The thing I'm wondering is if this is in any way useful. I imagine using it for a CMS and including a backend to control the site colors and such may be interesting but I'm not fully convinced.

    I wrote up the steps and added a sample project at my site here: http://imaginekitty.com/624/dynamic-...g-asp-net-mvc/

    Comments and criticisms welcome.

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,651
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Nice. Now tack on CSS compression and caching . . .

  3. #3
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    Nice. Now tack on CSS compression and caching . . .
    Yeah, caching was the next step but I'm not sure about CSS compression. Unless I write the whole file to the response stream instead of having a view. Hmm. Interesting thought.

    I'm still not 100&#37; convinced on CSS needing variables.

  4. #4
    SitePoint Guru pufa's Avatar
    Join Date
    Oct 2004
    Location
    Portugal, Lisboa
    Posts
    947
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginekitty View Post
    ...but I'm not sure about CSS compression. Unless I write the whole file to the response stream instead of having a view. .
    A Reponse Filter?
    Ciao, Rui...

  5. #5
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pufa View Post
    A Reponse Filter?
    Ahh!

    How's this:
    Code Csharp:
    		public override void Write(byte[] buffer, int offset, int count)
    		{
    			string html = System.Text.UTF8Encoding.UTF8.GetString(buffer).Replace("  "," ");
     
    			var repl = new Dictionary<string, string> { { "\r\n", String.Empty }, { "  ", " " }, { "{ ", "{" }, { " }", "}" }, { ";}", "}" }, { ": ", ":" }, { ", ", "," }, { "; ", ";" } };
    			html = repl.Aggregate(html, (s, r) => s.Replace(r.Key, r.Value));
     
    			html = Regex.Replace(html, @"/\*(.|\\s)*?\*/", string.Empty);
    			buffer = System.Text.UTF8Encoding.UTF8.GetBytes(html);
    			stream.Write(buffer, offset, count);
    		}
    It brought a 4.96kb file down to 2.74kb.
    Zip file on my site updated with this in the example.

  6. #6
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hmm. Is any of this even worth wasting the server's CPU cycles or tying up some memory using the cache? It was an interesting exercise but I just don't know if it's worth pursuing any further.

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,651
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Good question--that can really depend on app. I always try and keep a few major performance improvement ideas in the back pocket in case you need one real quick. You should just be able to use the normal ASP.NET MVC caching options though.

  8. #8
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    Good question--that can really depend on app. I always try and keep a few major performance improvement ideas in the back pocket in case you need one real quick. You should just be able to use the normal ASP.NET MVC caching options though.
    Yes, I used [OutputCache] in the example. It does consume a bit of memory, right? A few kb of cache isn't going to make or break anything though. I don't know if that means it's just not worth doing or that I may as well because it won't hurt anything.

    Here's the big question though. Since the browser is going to cache the file anyway, it will take it from browser cache instead of from the server after the first visit, right? OutputCaching will only apply for multiple simultaneous hits, right?

  9. #9
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Homerun solution. Kind of off topic, is MVC 2.0 Enterprise ready? Learning alot from this post alone...

  10. #10
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by USPatriot View Post
    Homerun solution.
    You're too kind. I honestly don't know that this project has any real world value.

    Quote Originally Posted by USPatriot View Post
    Kind of off topic, is MVC 2.0 Enterprise ready? Learning alot from this post alone...
    Version 1.0 is the one that comes up if you go to http://asp.net/mvc and download the installer. I believe version 2.0 is still a release candidate. There's nothing in this project that uses any Version 2.0 only items.

  11. #11
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    By homerun solution I mean, I have never heard of it before. If it is viable in the real-world, like you said, is up to debate. However, i'm the type of person that would rather build my own JavaScript framework, then use jQuery, but that would take ample time and resources. That is why I thought it was a cool idea, just takes imagination.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •