Copied to clipboard

Flag this post as spam?

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


  • David Zweben 268 posts 754 karma points
    Mar 14, 2012 @ 16:02
    David Zweben
    0

    Add currentPage's 'nodeName' as CSS class?

    Pretty simple: I'm trying to add the nodeName of the current page as a CSS class to that page's opening HTML tag. I would like to convert any spaces in the nodeName to underscores. I tried adding an XSLT file to the page template that selects the nodeName attribute on $currentPage, does a XSLT translate, and outputs that value, but I haven't been able to get it to work.

      <xsl:template match="/">

        <xsl:variable name="currentPage"/>

        <xsl:variable name="pageName" select="$currentPage/@nodeName"/>
        
        <xsl:variable name="newtext" select="translate($pageName,' ','_')"/>

        <xsl:value-of select="$newtext"/>

      </xsl:template>

    That code gives me the error System.Xml.Xsl.XslTransformException: Expression must evaluate to a node-set. 

    Does anyone know the proper way to do this, or what might be wrong with my code? I suspect my $pageName select is wrong, but I don't know why. Any help would be appreciated.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 14, 2012 @ 16:06
    Tom Fulton
    0

    Hi David,

    Try using umbraco.library:Replace instead of translate, ie:

    <xsl:variablename="newtext"select="umbraco.library:Replace($pageName,' ','_')"/>

    You could also just use @urlName instead of @nodeName, which will give you the friendly URL name which should already have spaces replaced with '-' by default, ie 'installing-modules'

    Also keep in mind that this might not be the best route, since the name of a page can change, then your CSS might not apply.  Not sure what exactly you're using this for though - maybe it makes sense to do it by template instead?

    HTH,
    Tom

  • David Zweben 268 posts 754 karma points
    Mar 14, 2012 @ 16:11
    David Zweben
    0

    Tom,

    Your latest comment got everything working as I was trying to do it, thanks!

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 14, 2012 @ 16:12
    Tom Fulton
    1

    Oh, sorry.  I just noticed you are declaring the currentPage variable yourself.  You don't need to do that, there should already be a line like this at the top of your XSLT file that gets populated automatically.  Try removing your variable and making sure this is at the top of your file...

    <xsl:param name="currentPage" />

    -Tom

  • David Zweben 268 posts 754 karma points
    Mar 14, 2012 @ 16:26
    David Zweben
    0

    Tom,

    I hadn't thought about the fact that the page name could easily change. What I'm trying to do is carry over a convention from when I hand-code, where I use the page name as a unique identifier for that page in CSS. You're right, perhaps it's better to go by template. I notice there's no 'templateName' attribute on a page, though. Is there a way I could get the current page's template *name* instead of the template ID number? If not, I guess I can just use the number.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 14, 2012 @ 16:37
    Tom Fulton
    0

    Not sure if you are using uComponents, but if you are, they have an XSLT Extension method for getting the template alias from it's ID.  This is what I've used in a past project:

    <xsl:value-of select="umbraco.library:Replace(Exslt.ExsltStrings:lowercase(ucomponents.cms:GetTemplateAlias($currentPage/@template)), ' ', '-')"/>

    and something like this in the template:

    <body class='<umbraco:Macro Alias="Page-BodyClass" runat="server"></umbraco:Macro>'>

    Note for the XSLT extension to work you need to add it to your /config/xsltExtensions.config file:

    <ext assembly="uComponents.Core" type="uComponents.Core.XsltExtensions.Cms" alias="ucomponents.cms" />

    And also add it to your namespace declarations at the top of your XSLT

    .... xmlns:ucomponents.cms="urn:ucomponents.cms"
    ....  exclude-result-prefixes=".... ucomponents.cms"

    -Tom

  • David Zweben 268 posts 754 karma points
    Mar 14, 2012 @ 16:38
    David Zweben
    0

    Ok, I'll look into that. Thanks for all your help.

Please Sign in or register to post replies

Write your reply to:

Draft