Copied to clipboard

Flag this post as spam?

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


  • Tony 11 posts 31 karma points
    Jun 11, 2011 @ 06:48
    Tony
    0

    Return only news with start dates later than today

    Posted before, but I wasn't clear and the system isn't letting me edit... so posting a new one here and trying to be more clear :)

    Being new to XSLT and Umbraco... I am having difficulty trying to not show a news article after it is past today's date.

    Here is the code I am using to show my news list...

    <xsl:if test="$numberOfItems &gt; 0">
        <ul id="newsList">
          
          <xsl:for-each select="$articlePages">
            
            <xsl:sort select="articleDate" order="ascending"/>

            <xsl:if test="position() > $recordsPerPage * number($pageNumber) and   
            position() &lt;= number($recordsPerPage * number($pageNumber) +   
            $recordsPerPage )">
      
              <li>
                <h3>
                  <href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:value-of select="@nodeName"/>
                  </a>
                </h3>
                <class="articleDate">
                  <xsl:value-of select="umbraco.library:FormatDateTime(articleDate,'dd MMMM yyyy')"/>
                </p>
                <p>
                  <xsl:value-of select="articleSummary"/>
                </p>
              </li>
            </xsl:if>
            
          </xsl:for-each>
        </ul>

        <xsl:if test="$numberOfItems > $recordsPerPage">
          <id="pager">
            <xsl:call-template name="pageNumbers">
              <xsl:with-param name="pageIndex" select="1"/>
            </xsl:call-template>
          </p>
        </xsl:if>
      
      </xsl:if>

    This code shows the news and pages it.  For the piece I am trying to add now, I have tried if statements which didn't work and for-each statements which all blew up in my face.

    Any help would be appreciated.

    Thank you,

    Tony

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 11, 2011 @ 10:05
    Chriztian Steinmeier
    1

    Hi Tony,

    There's a couple of handy extension methods in umbraco.library for that, e.g.:

    <xsl:for-each select="$articlePages[umbraco.library:DateGreaterThanToday(articleDate)]">

    or:

    <xsl:for-each select="$articlePages[umbraco.library:DateGreaterThanOrEqualToday(articleDate)]">

    /Chriztian

  • Tony 11 posts 31 karma points
    Jun 11, 2011 @ 21:19
    Tony
    0

    Thanks for the response Chriztian...

    I tried that before thinking it sounded like what I needed... I keep getting

    System.FormatException: String was not recognized as a valid DateTime

    Any idea why that would be happenening?  I have tried formatting the date and that doesn't seem to work, though I am not sure what exact format it is looking for.  Any ideas?

  • Tony 11 posts 31 karma points
    Jun 11, 2011 @ 21:33
    Tony
    0

    So here is what I find wierd...

    If I do this:
    <xsl:for-each select="$articlePages[umbraco.library:DateGreaterThanOrEqualToday('06/17/2011')]">
    it works... yay

    but if I do this:
    <xsl:for-each select="$articlePages[umbraco.library:DateGreaterThanOrEqualToday(umbraco.library:FormatDateTime(newsDate, 'MM/dd/yyyy'))]">
    it doesn't work... invalid date again

    or this:
    <xsl:variable name="dateNews" select="umbraco.library:FormatDateTime(newsDate, 'MM/dd/yyyy')"/>
    <xsl:for-each select="$articlePages[umbraco.library:DateGreaterThanOrEqualToday($dateNews)]">
    it doesn't work... invalid date again

    I can't hardcode the date because it needs to change per news item, so what am I missing here to make this work?

    Thanks!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 11, 2011 @ 22:07
    Chriztian Steinmeier
    1

    Hi Tony,

    If articleDate is a standard Date (or DateTime) property it should work. The only date format really worth storing is the xmlDate format (e.g., '2011-06-11T21:53:16+0200') - this will work with all the formatting functions AND it's sortable as just a string.

    What you need to be careful of though, is that when you do something like this:

    <xsl:for-each select="$articlePages[umbraco.library:DateGreaterThanOrEqualToday(newsDate)]">

    - the processor will call the function on ALL the matching $articlePages, whether they have a newsDate property or not; so maybe that's your problem - one of the pages doesn't have a newsDate property (or has a bad 'date' in it). This could easily happen if you add the newsDate property to a Document Type, after having already published pages based on the version without it.

    You can try to safeguard against the missing property issue by doing this:

    <xsl:for-each select="$articlePages[normalize-space(newsDate)][umbraco.library:DateGreaterThanOrEqualToday(newsDate)]"> 

    This will ensure that the there is a newsDate property and it has a value other than pure whitespace.

    /Chriztian

     

  • Tony 11 posts 31 karma points
    Jun 11, 2011 @ 22:28
    Tony
    0

    Seriously Chriztian... you are my hero.  That was the issue, so I used your "safeguard" code and it worked like a charm.

    Thank you very much... my headache is subsiding :)

    Tony

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 11, 2011 @ 23:52
    Chriztian Steinmeier
    0

    Great - you're welcome,

    One less problem in the world... :-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft