Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fourfinger 24 posts 44 karma points
    Mar 15, 2012 @ 11:46
    Fourfinger
    0

    Modify stylesheets in runtime

    Hello,

    I'm building a website that's supposed to have the ability to change the layout.

    The layout is based on 1 colorcode. This one is used in few places in the CSS.

    What I'm trying to to is create a usercontrol for example, in which the admin inserts a new color code (e.g "Green" or "#FFFFFF") and this would replace the color code in the css on the set positions.

    Is there any way to read/edit/save the css files in Umbraco?

     

    Thanks, Christophe

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 15, 2012 @ 13:53
    Tom Fulton
    0

    Hi,

    One thing you can do is use ApplicationBase to hook into the save event of your doctype that contains the color field, and onsave write the changes to the CSS file.  You can add some tokens in your CSS file and use regex to parse them.  I've successfully done this in a previous site and am thinking of turning it into a package, not sure on ETA though.

    -Tom

  • Rodion Novoselov 694 posts 859 karma points
    Mar 15, 2012 @ 15:43
    Rodion Novoselov
    1

    Hi. It seems that a more straightforward and simpler way to achieve it would be to add a color property to your content and implement a macro that will render an internal stylesheet (i.e. a <stylesheet> element) in the page, and than to insert a macro inside a <head> section of the template.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 15, 2012 @ 15:54
    Tom Fulton
    0

    Rodion's solution probably makes more sense in your case, didn't think of that :)

    In my case I went that route because it was using a complicated .LESS system like bootstrap with variables, so I was able to have AppBase update the one variable and have the color/style applied everywhere else.

  • Ian Robinson 79 posts 143 karma points
    Mar 16, 2012 @ 10:04
    Ian Robinson
    0

    I would go with Tom's solution if there are a lot of elements you need to change the colour on, but if there's only one or two, such as a just the h1 elements or only certain paragraph elements, well then another solution could involve using jQuery, but the page would only change if the user had javascript turned on.

    From your Razor or XSLT, you could output the custom colour as a javascript variable inside a script block, and then in your template you could have some jQuery code to check if the variable is not null, then use its value to change the colour of any specific elements on your page by explicitly stating them or giving the changable elements all the same class.

    e.g.  Output from Razor/XSLT script, if a custom colour is sepecified

    var customColour = "#FF0000";
    

    e.g.  Output from Razor/XSLT script, if NO custom colour is sepecified

    var customColour = null;
    

    e.g. jQuery inside your page template:

    $(document).ready(function() {
      if (customColour != null) {
    $("h1").css("color", customColour);
    $("p.customColour").css("color", customColour);
    }
    });

     

  • Fourfinger 24 posts 44 karma points
    Mar 20, 2012 @ 09:52
    Fourfinger
    0

    All great solutions guys! Thanks!

     

    Sorry for the late response though. Tom's way seems kinda a bit too difficult as I'm pretty new at Umbraco.

    Thanks anyway, I'm going with Ian's solution!

     

    Love this community ^^

Please Sign in or register to post replies

Write your reply to:

Draft