简体   繁体   中英

How to display “number of new” child pages Umbraco XSLT

I have two separate pieces of code I'm trying to combine.

The first counts the number of child pages and displays a number:

eg 8 child pages (or child page, if only 1 page)

<xsl:choose>
<xsl:when test="count(child::DocTypeAlias) &gt; 1">
    <p><xsl:value-of select="count(child::DocTypeAlias)"/> child pages</p>
</xsl:when>
<xsl:otherwise>
    <p><xsl:value-of select="count(child::DocTypeAlias)"/> child page</p>
</xsl:otherwise>

The code detects if the page was created within the last 30 days:

<xsl:variable name="datediff" select="umbraco.library:DateDiff(umbraco.library:ShortDate(umbraco.library:CurrentDate()), umbraco.library:ShortDate(@createDate), 'm')" />
    <xsl:if test="$datediff &lt; (1440 * 30)">
        <p>New</p>
    </xsl:if>

I want to combine them so I can get a count of child pages and a count of the "new" pages.

eg 8 child pages - 2 new pages

I've tried the following but it doesn't return the correct values:

<xsl:variable name="datediff" select="umbraco.library:DateDiff(umbraco.library:ShortDate(umbraco.library:CurrentDate()), umbraco.library:ShortDate(@createDate), 'm')" />
    <xsl:choose>
        <xsl:when test="$datediff &lt; (1440 * 30) and count(child::DocTypeAlias) &gt; 1">
            <p><xsl:value-of select="$datediff &lt; (1440 * 30) and count(child::DocTypeAlias)"/> new pages</p>
        </xsl:when>
        <xsl:otherwise>
            <p><xsl:value-of select="$datediff &lt; (1440 * 30) and count(child::DocTypeAlias)"/> new page</p>
        </xsl:otherwise>
    </xsl:choose>

It returns: "true new pages" I don't know how to get it to display the number (2 new pages).

Can anyone help? Cheers, JV

Look carefully at the contents you are specifying for the paragraph:

<p>
  <xsl:value-of select="
    $datediff &lt; (1440 * 30) 
    and 
    count(child::DocTypeAlias)"/> 
  new pages
</p>

You have an and with a Boolean as the left argument and an integer as the right argument. Put yourself in the processor's shoes: doesn't it look a lot as if you were asking it to calculate a Boolean value?

Since this expression is enclosed in a when element that already tests for the date difference, you (almost certainly) don't need to repeat the comparison of $datediff to 43200. (I say "almost certainly" because I don't think I understand the logic of your application in detail, so I could be wrong.) I suspect what you want to be saying is:

<p>
  <xsl:value-of select="count(child::DocTypeAlias)"/> 
  new pages
</p>

You'll need an analogous change in the otherwise .

With many thanks to Chriztian Steinmeier:

 <!-- The date calculation stuff -->
<xsl:variable name="today" select="umb:CurrentDate()" />
<xsl:variable name="oneMonthAgo" select="umb:DateAdd($today, 'm', -1)" />

<!-- Grab the nodes to look at -->
<xsl:variable name="nodes" select="$currentPage/DocTypeAlias" />

<!-- Pages created within the last 30 days -->
<xsl:variable name="newPages" select="$nodes[umb:DateGreaterThanOrEqual(@createDate, $oneMonthAgo)]" />
<xsl:template match="/">
  <xsl:choose>
    <xsl:when test="count($newPages)">
      <p> <xsl:value-of select="count($newPages)" /> <xsl:text> New Page</xsl:text>
        <xsl:if test="count($newPages) &gt; 1">s</xsl:if>
      </p>
    </xsl:when>
    <xsl:otherwise>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM