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:
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 > 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>
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!
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: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>
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...
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?
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
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>
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
Looks like this line should read something like:
<xsl:variable name="titleSuffixOnChildPages" select="$currentPage/ancestor-or-self::* [@isDoc and string-length(titleSuffixOnChildPages) > 0]" />
Hi guys, thanks for quick replies!
Changed the code to this, and it seems to work =)
Great! Glad to help.
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!
Hi Axel,
This is a somewhat different take on this, which is (for me) more XSLT'ish (as if that's a word :-)
Feel free to comment on how it works if you want.
/Chriztian
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!
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
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.
Anyone? ;)
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.:
Why the class isn't showing up, we'll ahve to investigate a little further - code to add it seem OK from here...
/Chriztian
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?
*bump* ;)
Still cant figure it out :(
Hi Axel,
Could you describe the scenarios and how you want it to work, e.g. something like this:
/Chriztian
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:
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 :)
Hi Axel - you're welcome to send me an e-mail at (firstname) @ (lastname) .dk
/Chriztian
is working on a reply...