Copied to clipboard

Flag this post as spam?

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


  • Alex Burr 77 posts 128 karma points
    Mar 10, 2011 @ 15:13
    Alex Burr
    0

    Change output markup conditionally based on page data

    Hi everyone! 

    I'm having a brain fart moment. I've done things like this in the past but for some reason I can't wrap my head around it today.

    Here's what I'm doing: Pages include an optional property called pageSubheading, which is a Textstring. At the top of each page, I display the page title in an <h1> tag, but if they also include a pageSubheading, I will output that in an <h2> immediately below and wrap them both in an <hgroup> tag. Here's my XSL:

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    
    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
    
        <xsl:choose>
            <xsl:when test="string($currentPage/data[@alias='pageSubheading'])!=''">
                <hgroup>
                    <h1><xsl:value-of select="$currentPage/@nodeName" /></h1>
                    <h2><xsl:value-of select="$currentPage/data[@alias='pageSubheading']" /></h2>
                </hgroup>
            </xsl:when>
            <xsl:otherwise>
                <h1><xsl:value-of select="$currentPage/@nodeName" /></h1>
            </xsl:otherwise>
        </xsl:choose>
    
    </xsl:template>
    
    What am I missing here? Thanks in advance.
  • kows 81 posts 151 karma points c-trib
    Mar 10, 2011 @ 15:24
    kows
    0

    W3C: The <xsl:when> element is used in conjunction with <xsl:choose> and <xsl:otherwise> to express multiple conditional tests.

     

    Don't you need xsl:if ? 

    edit:

    I would try to solve this by entering simple templates to the when/otherwise (just a letter),

    and add each statement back line by line to see what goes wrong. Does it display any line at all or just error?

  • Alex Burr 77 posts 128 karma points
    Mar 10, 2011 @ 15:47
    Alex Burr
    0

    I chose to use <xsl:when> since there is no such thing as "else" in XSL.

    To do some debugging, I tried this. I tested on a page that *had* some text in pageSubheading.

    <xsl:choose>
            <xsl:when test="string($currentPage/data[@alias='pageSubheading'])!=''">
                <hgroup>
                    <h1><!-- <xsl:value-of select="$currentPage/@nodeName" /> --> Pass</h1>
                    <h2><xsl:value-of select="$currentPage/data[@alias='pageSubheading']" /></h2>
                </hgroup>
            </xsl:when>
            <xsl:otherwise>           
                <h1><!-- <xsl:value-of select="$currentPage/@nodeName" /> --> Fail</h1>
            </xsl:otherwise>
        </xsl:choose>
    
    The output was <h1> Fail</h1>, which was surprising.
    Then I modified the XSL to this to try again:
    <xsl:choose>
            <xsl:when test="string($currentPage/data[@alias='pageSubheading'])!='False'">
                <hgroup>
                    <h1><!-- <xsl:value-of select="$currentPage/@nodeName" /> --> Pass</h1>
                    <h2><xsl:value-of select="$currentPage/data[@alias='pageSubheading']" /></h2>
                </hgroup>
            </xsl:when>
            <xsl:otherwise>           
                <h1><!-- <xsl:value-of select="$currentPage/@nodeName" /> --> Fail</h1>
            </xsl:otherwise>
        </xsl:choose>
    
    And this time the output was <hgroup><h1> Fail</h1><h2></h2></hgroup>. There was no output between the <h2> tags.
    So it seems like my conditional <xsl:when> statement is wrong? Like I said, I've done this type of thing before... in fact I copied the XSL directly from an earlier project that had done something similar. So I guess I'm confused as to how to reference this properly.
  • kows 81 posts 151 karma points c-trib
    Mar 10, 2011 @ 16:22
    kows
    0

    <hgroup><h1> Fail</h1><h2></h2></hgroup>

    how's that possible, "Fail" is in your otherwise tag. while the structure is in your when.

    Could it be that you must drop $currentPage(/)  since you're in a template?

  • Alex Burr 77 posts 128 karma points
    Mar 10, 2011 @ 16:31
    Alex Burr
    0

    That's what I'm saying; I appear to be getting the opposite result? Now I've tried this...

     

    <xsl:when test="string($currentPage/data[@alias='pageSubheading'])=''">
        <hgroup>
            <h1><xsl:value-of select="$currentPage/@nodeName" /></h1>
            <h2><xsl:value-of select="$currentPage/data[@alias='pageSubheading']" /> Pass</h2>
            </hgroup>
    </xsl:when>
    
    And what I get is <hgroup><h1>(PAGE TITLE)</h1><h2> Pass</h2></hgroup>

     

  • kows 81 posts 151 karma points c-trib
    Mar 10, 2011 @ 16:43
    kows
    0

    could you try testing with $currentPage/pageSubheading ?

  • Alex Burr 77 posts 128 karma points
    Mar 10, 2011 @ 16:48
    Alex Burr
    0

    I've changed to this and it's now working:

    <xsl:choose>
    <xsl:when test="$currentPage/pageSubheading!=''">
    <hgroup>
    <h1><xsl:value-of select="$currentPage/@nodeName" /></h1>
    <h2><xsl:value-of select="$currentPage/pageSubheading" /></h2>
    </hgroup>
    </xsl:when>
    <xsl:otherwise>
    <h1><xsl:value-of select="$currentPage/@nodeName" /></h1>
    </xsl:otherwise>
    </xsl:choose>
    I'm a little confused as to why that worked, but the end result is that it worked.
    I really appreciate the help. Now on to figuring out my navigation/breadcrumb problems in a diffrerent topic!
    Thanks again.

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Mar 10, 2011 @ 16:58
    Chriztian Steinmeier
    0

    Hi Alex,

    This is because your initial code was using the old XML Schema (and would work with a site using that), whereas the working code use the new (as of Umbraco 4.5) XML Schema, which is much better to work with.

    /Chriztian

     

  • Alex Burr 77 posts 128 karma points
    Mar 10, 2011 @ 17:01
    Alex Burr
    0

    Ah, yes, I had built the old code on a site using an earlier version of Umbraco. The site I'm building now is on 4.5.0.

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft