Copied to clipboard

Flag this post as spam?

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


  • NtJensen 5 posts 25 karma points
    Nov 10, 2011 @ 14:17
    NtJensen
    0

    Change class on div when url matches $currentPage

    Hi, This is my second website that im using umbraco for, and this time I decided that I wanted to try out the dynamic menu using the xslt.

    an alpha of the site can be seen at: http://alpha.yilmas.dk

    This all worked well, the menu is dynamic relative to the pages and all, but I want to change each menu items class when the following is true "current page = selected page".. So I tried this out for size:

    <xsl:choose>
      <xsl:when test="$currentPage=umbraco.library:NiceUrl(@id)">
      <div class="btnLeftSelect">&nbsp;</div>
      </xsl:when>
      <xsl:otherwise>
      <div class="btnLeftDeSelect">&nbsp;</div>
      </xsl:otherwise>
    </xsl:choose>

    Im using the template "List Sub Pages by Level".. So I was guessing that $currentPage would give me what I needed...

    Also note that this is the first time that I've touched XSLT, normally I would do this in a usercontrol, but wanted to test the possibiliies in XSLT..

    So can anyone help me to achieve what I want ?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 10, 2011 @ 14:23
    Tom Fulton
    0

    Hi,

    You don't need to compare the URLs but just the ID of the currentPage vs the page in your loop, try this:

    <xsl:choose>
    <xsl:when test="$currentPage/@id = @id"> <!-- compares the currentPage's ID to the id of the current page in your loop -->
      <div class="btnLeftSelect">&nbsp;</div>
      </xsl:when>
      <xsl:otherwise>
      <div class="btnLeftDeSelect">&nbsp;</div>
      </xsl:otherwise>
    </xsl:choose>

    Also here's another way to write it, maybe simpler:

    <div class="btnLeftSelect">
      <xsl:if test="$currentPage/@id = @id"><xsl:attribute name="class">btnLeftDeSelect</xsl:attribute></xsl:if>
      &nbsp;
    </div>

    Hope this helps,
    Tom

  • NtJensen 5 posts 25 karma points
    Nov 10, 2011 @ 14:29
    NtJensen
    0

    Thank you very much... Works like a charm... I used your first solution, as for me, it seemed easier..

    Though I will save both solutions, might need one of them some day...

  • NtJensen 5 posts 25 karma points
    Nov 10, 2011 @ 15:16
    NtJensen
    0

    One more question...

    In my News("Nyheder" as seen in the link) menu. Here I would like it to keep the News menu highlighted, even though its not actually on that page but on a sub page...

    So you click on news(Nyheder), after that you click on a news item in the body, from here you will be redirected to the specific news page. On this page I would like that the News menu icon keeps being highlighted through the before mentioned class.. Would this be possible through the xslt without the need of something big?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 10, 2011 @ 15:39
    Tom Fulton
    0

    Yep, you can do that, I think this should work:

    <xsl:when test="$currentPage/@id = ./ancestor-or-self::* [@isDoc]/@id"> <!-- compares the currentPage's ID to the ID of the current page in your loop and it's ancestors -->
  • NtJensen 5 posts 25 karma points
    Nov 10, 2011 @ 15:59
    NtJensen
    0

    Thx, works like a charm...

    Instead of flooding the forum with new topics when ever there is a problem, I'll just continue in this thread, even though the topic title doesn't contain it... (If you want me to I can create a seperate thread though)...

    This is my xml:

    <RMTNews id="1136" lalallalalalalallalalalalalalal isDoc="">
    <newstitle>Nyheder</newstitle>
       <RMTNewsItem id="1137" lalalalalla nodeName="News Item 1" morelalalla isDoc="">
           <newsitemtitle>Ringsted Morskabsteater er nu i alpha!</newsitemtitle>
           <newsitemtext>
              <p>Vi har den glæde at præsenterer jer for den nye hjemmeside for Ringsted Morskabsteater.</p>
           </newsitemtext>
           <newsitemowner>0</newsitemowner>
           <newsitemdate>2011-11-10T00:00:00</newsitemdate>
        </RMTNewsItem>
    </RMTNews>

    I can get the nodeName without any problems... But I would also like to get the newsitemdate value and ofcourse format it to something readable...
    This is what I tried, and failed...(I normally hate XML, not how its written, but the stupidity of the access <- in my mind that is) Though I love the simplicity...

    <a href="{umbraco.library:NiceUrl(@id)}">
        <xsl:value-of select="@nodeName"/>
        <xsl:value-of select="@newsitemdate" />
    </a>

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 10, 2011 @ 16:19
    Tom Fulton
    0

    Hi,

    Not a problem :)

    When you use the @ sign you are asking for an attribute of your node, for example id, nodeName, etc.  The attributes are typically the built-in properties of Umbraco (sortorder, path, id, createDate, etc)

    To get a custom property (which are elements under your node rather than attributes), just omit the @ sign. 

    <xsl:value-of select="newsitemdate"/>

    Next, to format the date you can use an extension method FormatDateTime

    <xsl:value-of select="umbraco.library:FormatDateTime(newsitemdate, 'MM/dd/yyyy)"/>

    Hope this helps,
    Tom

  • NtJensen 5 posts 25 karma points
    Nov 10, 2011 @ 16:36
    NtJensen
    0

    Thx m8, again...

    Could you tell me what I must write in this forum to generate those code boxes?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 10, 2011 @ 16:39
    Tom Fulton
    0

    Sure :)

    Just type out your text, highlight it, then click on the dropdown that says Paragraph and select Preformatted

Please Sign in or register to post replies

Write your reply to:

Draft