Copied to clipboard

Flag this post as spam?

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


  • Cameron 23 posts 42 karma points
    Aug 28, 2012 @ 04:10
    Cameron
    0

    How can I make a CSS content dynamic.

    Hi all,

    have been scratching the head over this for a while and have come up blank.

    The content my user creates (generated via XSLT) looks like this....

     

    <div id="wrapper">
      <ul class="nav">
        <li class="home"><a class="active" href="index.html">Home</a></li>
        <li class="investments"><a href="investments.html">Investments</a></li>
        <li class="investors"><a href="investors.html">Investors</a></li>
        <li class="announcements"><a href="newsmain.html">News &amp;
          Announcements</a></li>
        <li class="contact"><a href="contactus.html">Contact Us</a></li>
      </ul>
    </div>
    Obviously the class "active" is the current page.
    The classes 'home', 'investments' etc map to page name and in our css we have snippets that look like this....

    .nav li.investments a:link, .nav li.investments a:visited {
      background-image:url(../img/nav-investments-img-sprite.png);
      background-repeat:no-repeat;
      background-position: 25px 0px;

    This is obviously for styling the "investments" menu item.

    As you can see, there is a background image used which is specific to each item.

    What I need to be able to do is generate this output for the style sheet dynamically.

    The customer wants:

    1. to let the content editors choose what the icon images will be
    2. no styling within the HTML
    3. no reference to the background image within the HTML.
    4. no javascript

    How can I achieve this?

    I cannot see any way to obviously create a CSS file from XSLT (or other ways). 

    All help appreciated!

    Thanks,

    Cam

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 28, 2012 @ 07:50
    Fuji Kusaka
    0

    Hi  Cam,

    One possible way to achieve this would be by making some chances to your xslt and css stylesheet.

    Instead of calling your class="home" or class="investments" you could simply name it item-1, item-2.

    Then in your xslt have something like this which will add the count 1,2,3 etc to your class since the navigation is dynamic.

    <div id="wrapper">
    <ul class="nav">
    <xsl:for-each select="....">// Whatever method you are pulling your navigation
     <xsl:element name="li">
    <xsl:attribute name="class">item-<xsl:value-of select="position()"/></xsl:attribute>
    </xsl:element>
    </xsl:for-each>
    </ul>
    </div> 

    You can give this a try.

    //Fuji

  • Cameron 23 posts 42 karma points
    Aug 28, 2012 @ 08:23
    Cameron
    0

    I do not understand how this would enable me to dynamically create CSS at all. It's just the HTML content which I have no issue with.

    What I need is to have the page content image used in CSS ....

    .nav li.home a:link,.nav li.home a:visited {
      background
    -image:url(../img/nav-home-img-sprite.png);
      background
    -repeat:no-repeat;
      background
    -position:25px0px;
    .nav li.investments a:link,.nav li.investments a:visited {
      background
    -image:url(../img/nav-investments-img-sprite.png);
      background
    -repeat:no-repeat;
      background
    -position:25px0px;

    This way when someone comes up with the next great new nav item and image that is to be used, it is only a matter of changing content, not the CSS.

    We don't let anyone near the CSS....

     

    .nav li.next_new_idea a:link,.nav li.next_new_idea a:visited {
      background
    -image:url(../img/nav-next_new_idea-img-sprite.png);
      background
    -repeat:no-repeat;
      background
    -position:25px0px;

     

     


  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 28, 2012 @ 08:51
    Fuji Kusaka
    0

    Hi Cam, 

    Sorry i miss understood you there. So you want to be able to change the css class of <li>  at anytime right? 

  • Cameron 23 posts 42 karma points
    Aug 28, 2012 @ 08:58
    Cameron
    0

    Well yes, but I also want there to be many variant of them, all driven via the content pages.

    Each page, as part of it's content will have an ICON image.

    References to this image should be inserted into the CSS, so that for each menu item there is a new class and a new CSS entry and background image associated with it.

  • Dan 1285 posts 3917 karma points c-trib
    Aug 28, 2012 @ 10:28
    Dan
    0

    Hi Cameron,

    So you want dynamic CSS that's not inline, right?  There are two options I think...

    Firstly, you could write an XSLT macro which outputs your CSS dynamically as plain text, then include it at the top of your page in <style type="text/css"></style> tags.  i.e. An embedded style sheet.  This should be simple to do but it does mean that the CSS is on the page (embedded), albeit separate from the mark-up.  You just need to make sure the class names generated for the CSS tie up with the class names in your HTML (they could be based on page URL or node id or position, as per Fuji's post above - so long as they're unique to the node and valid syntax it doesn't matter).

    Secondly, if you want to go a step further and use a linked style sheet, you should be able to do this by changing the content type in your XSLT macro to serve the content as text/css, like this

        <xsl:value-of select="umbraco.library:ChangeContentType('text/css')"/>

    So you'd create a content node which calls a template which contains nothing but a call to the macro (not even any white space in the template, just a macro call) which outputs the CSS content with the correct content type.  You could use an alternative template to call this from any content node.

    I've not got time right now to drum up a full example, but post back here if you get stuck and I'll try to have a look later today.  Hopefully this points you in the right direction in the meantime though.

  • Cameron 23 posts 42 karma points
    Aug 28, 2012 @ 10:33
    Cameron
    0

    Hi Dan. 

     

    I think the second option is what I was after

    An obvious solution when you think about it.

    Excellent. I'll give it a go when I get back in on Thursday. 

     

    Cheers.

  • Cameron 23 posts 42 karma points
    Aug 31, 2012 @ 10:47
    Cameron
    0

    So I manage to get this working using the second method as outlined above - thanks Dan.

    The only "issue" as such is that my style sheet file is named as an ASPX file rather than a CSS file.

    Any ideas as to how to fix this? Would rather it be a CSS file if possible. Tried playing around with URL rewriting but with no success.

    Cheers,

    Cam

Please Sign in or register to post replies

Write your reply to:

Draft