Copied to clipboard

Flag this post as spam?

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


  • Axel S 12 posts 33 karma points
    Jan 27, 2011 @ 16:17
    Axel S
    0

    Closest parent node that has a property set

    Hello!

    Normally I have the <title> field as "node name - site name". This works fine.

    However, I have a subsection of my site that has a different branding, so for a page I want to be able to specify an alternative suffix than site name. I want to apply this custom suffix not to this page itself, but all child pages.

    I am new to Umbraco and XSLT, so I haven't figured out how to find the closest parent that has the titleSuffixOnChildPages property set, and how to display the value if found!

    All help appriciated, this is what I've tried so far:


    <xsl:param name="currentPage"/>
        
    <xsl:variable name="homePage" select="$currentPage/ancestor-or-self::* [@level=1]" />
    <xsl:variable name="titleSuffixOnChildPages" select="$currentPage/ancestor-or-self::* [data [@alias = 'titleSuffixOnChildPages'] != '']" />
           
      <xsl:template match="/">
        <title>
          
          <xsl:choose>
          <xsl:when test="$currentPage/pageTitleField != ''">
            <xsl:value-of select="$currentPage/pageTitleField"/>
          </xsl:when>

          <xsl:value-of select="$currentPage/@nodeName"/>

          <xsl:choose>
            <xsl:when test="$titleSuffixOnChildPages != ''">
              <xsl:text> - </xsl:text><xsl:value-of select="$titleSuffixOnChildPages"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text> - </xsl:text><xsl:value-of select="$homePage/siteName"/>
          </xsl:otherwise>
          </xsl:choose>
          
        </title>
      </xsl:template>

  • Rich Green 2246 posts 4008 karma points
    Jan 27, 2011 @ 17:34
    Rich Green
    0

    Hi,

    What version of Umbraco are you using, you seem to have mixed the XML schemas up a little (this changed at version 4.5)

    Rich

  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Jan 27, 2011 @ 18:18
    Jason Prothero
    0

    Looks like this line should read something like:

    <xsl:variable name="titleSuffixOnChildPages" select="$currentPage/ancestor-or-self::* [@isDoc and string-length(titleSuffixOnChildPages) &gt; 0]" />

     

  • Axel S 12 posts 33 karma points
    Jan 27, 2011 @ 20:11
    Axel S
    0

    Hi guys, thanks for quick replies!

    Changed the code to this, and it seems to work =)

    <xsl:variable name="homePage" select="$currentPage/ancestor-or-self::* [@level=1]" />
    <xsl:variable name="titleSuffixOnChildPages" select="$currentPage/ancestor::* [@isDoc and string-length(titleSuffixOnChildPages) &gt; 0]" />
        
      <xsl:template match="/">
        <title>
          
          <xsl:choose>
          <xsl:when test="$currentPage/pageTitleField != ''">
            <xsl:value-of select="$currentPage/pageTitleField"/>
          </xsl:when>
            
          <xsl:when test="$homePage/@id = $currentPage/@id">
            <xsl:value-of select="$currentPage/siteName"/>
          </xsl:when>
          <xsl:otherwise>
              <xsl:value-of select="$currentPage/@nodeName"/>
          </xsl:otherwise>
          </xsl:choose>
          
          <xsl:if test="$currentPage/@level &gt; 1" >

          <xsl:choose>
            <xsl:when test="$titleSuffixOnChildPages != ''">
              <xsl:text> - </xsl:text><xsl:value-of select="$titleSuffixOnChildPages/titleSuffixOnChildPages"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text> - </xsl:text><xsl:value-of select="$homePage/siteName"/>
          </xsl:otherwise>
          </xsl:choose>
            
           </xsl:if>
          
        </title>
      </xsl:template>
  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Jan 27, 2011 @ 20:24
    Jason Prothero
    0

    Great!  Glad to help.

  • Axel S 12 posts 33 karma points
    Jan 27, 2011 @ 20:26
    Axel S
    0

    Thx :) Forgot to answer about the verison - it is 4.5.2. Feel free to point out any mistakes/bad code, and I'll correct it!

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Jan 27, 2011 @ 21:46
    Chriztian Steinmeier
    1

    Hi Axel,

    This is a somewhat different take on this, which is (for me) more XSLT'ish (as if that's a word :-)

    <xsl:variable name="homePage" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
    <!-- Grabs the value from the first ancestor that has a value in titleSuffixOnChildPages -->
    <xsl:variable name="titleSuffixOnChildPages" select="$currentPage/ancestor::*[normalize-space(titleSuffixOnChildPages)][1]/titleSuffixOnChildPages" />
    
    <xsl:template match="/">
        <!-- Grab pageTitleField if present and not empty -->
        <xsl:variable name="pageTitleField" select="$currentPage/pageTitleField[normalize-space()]" />
    
        <!-- Grab siteName (if this is homePage and not empty) -->
        <xsl:variable name="siteName" select="$currentPage[@id = $homePage/@id]/siteName[normalize-space()]" />
    
        <title>
            <!-- Write the first of these that has a value -->
            <xsl:value-of select="($pageTitleField | $siteName | @nodeName[not($pageTitleField | $siteName)])[1]" />
            <xsl:if test="$currentPage/@level &gt; 1">
                <!-- Not on the homePage so add the suffix specified or the siteName -->
                <xsl:value-of select="concat(' - ', ($titleSuffixOnChildPages | $homePage/siteName[not($titleSuffixOnChildPages)])[1])" />
            </xsl:if>
        </title>
    </xsl:template>
    

    Feel free to comment on how it works if you want.

    /Chriztian 

  • Axel S 12 posts 33 karma points
    Jan 27, 2011 @ 22:12
    Axel S
    1

    This is my first meeting with the Umbraco community, so fist I want to say that I'm very happy for the quick and through responses =)

    Your code looks much nicer indeed Chriztian. I tested it, and it worked, except that nodeName didn't show. I changed @nodeName to $currentPage/@nodeName and it seems to work perfect!

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Jan 27, 2011 @ 22:15
    Chriztian Steinmeier
    0

    Ha - I left that one in there for you to find...  :-) [not :(]

    Welcome to the awesome world of Umbraco, then - you're gonna like the vibe here!

    /Chriztian

  • Axel S 12 posts 33 karma points
    Jan 28, 2011 @ 09:44
    Axel S
    0

    Hehe!

    I have a somewhat related newbie page parent question. I realise that I need to read some XSLT litterature to understand this properly, I promise to do that my next holiday ;)

    I have a third level navigation bar, and want to add the "local home" (the level above) as the first element. Basically the same as getting the home link on the top level menu, but that was easier to achive by adding a <a href="/">Home</a>...

    The code below was the best I could do. The ID link on the home item is correct, but that's about it. Trying to add a "umbraco.library:NiceUrl" on the ID caused a "System.OverflowException" error. And the current class is not added when on the page.


        <xsl:variable name="level" select="3"/>
        
        <xsl:variable name="localHomePage" select="$currentPage/ancestor-or-self::*[@level = 3]" />

        <xsl:template match="/">
            <ul class="thirdLevel">
             
              <li>
                 <a href="{$localHomePage/@id}">
                    <xsl:if test="$localHomePage/@id = $currentPage/@id">
                    <!-- we're under the item - set class to current for the li element -->
                    <xsl:attribute name="class">current</xsl:attribute>
                  </xsl:if>
                   <xsl:value-of select="umbraco.library:GetDictionaryItem('home')"/></a>
                </li>
              
              <xsl:for-each select="$currentPage/ancestor-or-self::* [@level >= $level]/* [string(showInSubNavigation) = '1' and @isDoc]">
                <li>
                  <xsl:if test="$currentPage/ancestor-or-self::*/@id = current()/@id">
                    <!-- we're under the item - set class to current for the li element -->
                    <xsl:attribute name="class">current</xsl:attribute>
                  </xsl:if>
                  <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>
                </li>
              </xsl:for-each>
            </ul>
        </xsl:template>
  • Axel S 12 posts 33 karma points
    Jan 31, 2011 @ 04:22
    Axel S
    0

    Anyone? ;)

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Jan 31, 2011 @ 09:07
    Chriztian Steinmeier
    0

    Hi Axel,

    I'm guessing the OverflowException occurs when you try to save, right?

    The snippet assumes that it's run on a page on or below level 3, but when you hit Save in the editor, Umbraco will run the transform with the Content node as $currentPage, which results in your $localHomePage variable returning an empty set (thus, the exception when trying to call NiceUrl()).

    You can fix it by wrapping an if statement around your code, e.g.:

    <xsl:if test="$localHomePage"> ... <xsl:if>

    Why the class isn't showing up, we'll ahve to investigate a little further - code to add it seem OK from here...

    /Chriztian

  • Axel S 12 posts 33 karma points
    Jan 31, 2011 @ 09:59
    Axel S
    0

    Yep, that error. Thanks, the if test did the trick! And just noticed that I added the class attribute to the A tag instead of the LI tag. So that works as well!

    But now I discovered that this obviously works where "home" in the menu is level 3 and the subitems level 4. Other places in the structure this menu is also used where "home" is level 4 and subitems level 5.

    I guess we have to some way find out what's the first menu item and use the level above this as the localhomepage link?

  • Axel S 12 posts 33 karma points
    Feb 05, 2011 @ 04:54
    Axel S
    0

    *bump* ;)

    Still cant figure it out :(

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Feb 05, 2011 @ 16:39
    Chriztian Steinmeier
    0

    Hi Axel,

    Could you describe the scenarios and how you want it to work, e.g. something like this:

    Content
    - Section A  <-- localHome for A1+A2
    --- Page A1
    --- Page A2
    - Section B
    --- SubSection BC  <-- localHome for BC1+BC2
    ----- Page BC1
    ----- Page BC2
    - Section C  <-- localHome for C1+C2
    --- Page C1
    --- Page C2

    /Chriztian

     

  • Axel S 12 posts 33 karma points
    Feb 05, 2011 @ 17:12
    Axel S
    0

    Thanks for your patience! The submenu is not shown on the first menu level, but sometimes on the second and in other cases on the third. Something like this:

    Content

    - Section A
    --- Subsection AB1 <-- localHome for AC1+A2
    ----- Page AB1
    ----- Page AB2
    --- Subsection AC2 <-- localHome for AC1+A2
    ----- Page AC1
    ----- Page AC2

    - Section B
    --- SubSection BC1
    ----- Subsubsection BCA1 <-- localHome for BCA1+BCA2
    ------- Page BCA1
    ------- Page BCA2
    ----- Subsubsection BCB1 <-- localHome for BCB1+BCB2
    ------- Page BCB1
    ------- Page BCB2

    Is it possible to send a PM from this forum? In that case I can send you the link to the site in question and show the two scenarios :)

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Feb 05, 2011 @ 21:49
    Chriztian Steinmeier
    0

    Hi Axel - you're welcome to send me an e-mail at (firstname) @ (lastname) .dk

    /Chriztian

     

Please Sign in or register to post replies

Write your reply to:

Draft