I’ve been working with Umbraco quite a bit lately and I’m really loving it as a content management framework. I’ve already converted GeekGive.org, GeekRoadTrip.com, and I’m working on a few others. Being a developer though I often find myself outside the box and today is no different.
I’m building a site where the homepage will actually reflect content from a subpage including the title. This means I have to get rid of my standard title block in the template and use XSLT.
My standard title template block:
<title><asp:placeholder runat="server"><umbraco:Item field="pageName" runat="server"></umbraco:Item> - <umbraco:Item field="siteName" recursive="true" runat="server"></umbraco:Item></asp:placeholder></title>
I created a macro and set about to define the title text. Then it hit me, how do I recursively get the siteName field I defined on the homepage? It turns out this is really simple to do using XSLTs ancestor-or-self function in the value-of select:
$currentPage/ancestor-or-self::node [string(data[@alias='siteName'])!=''] [1] /data[@alias='siteName']
The full macro is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:template match="/">
<xsl:if test="$currentPage/data [@alias = 'pageTitle'] != ''">
<xsl:value-of select="$currentPage/data [@alias = 'pageTitle']"/>
-
</xsl:if>
<xsl:value-of select="$currentPage/ancestor-or-self::node [string(data[@alias='siteName'])!=''] [1] /data[@alias='siteName']" />
</xsl:template>
</xsl:stylesheet>
Hope this helps!