Copied to clipboard

Flag this post as spam?

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


  • Chad Rosenthal 272 posts 474 karma points
    Jun 16, 2010 @ 02:29
    Chad Rosenthal
    0

    Trying to create a complex apply-template.

    Trying to create an events module that uses categories from a checkboxlist. Currently, I have the following code:

                <xsl:variable name="numberOfArticles" select="count($currentNode/descendant::node//node [@nodeTypeAlias = $itemTemplate and umbraco.library:FormatDateTime(data [@alias=$itemStartDateTime], $requestedMonthYearFormat) = $formattedRequestDate])" />
            <xsl:choose>
                <xsl:when test="$numberOfArticles = 0">
                    <li>
                        <h3>No Events to Display</h3>
                        <p>The section you are viewing contains no events.</p>
                    </li>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="$rootNode/descendant-or-self::node//node [@nodeTypeAlias = $itemTemplate and umbraco.library:FormatDateTime(data [@alias=$itemStartDateTime], $requestedMonthYearFormat) = $formattedRequestDate]" mode="listing">
                        <xsl:sort order="ascending" select="data[@alias = $itemStartDateTime]" data-type="text"/>
                    </xsl:apply-templates>
                    <img src="../images/level2/divider_dotted_short.gif" width="416" height="10" align="middle" class="divdot"/>[<a href="#top" class="backToTop">Back to top</a>] <br />
                </xsl:otherwise>
              </xsl:choose>
    

    This works great to create the events module without using the categories. To see if the event has the correct categories, I use the following code within the listing template:

          <xsl:variable name="isEventPartOfCategory">
              <xsl:choose>
              <xsl:when test="$categories != ''">
                  <xsl:variable name="searchvalues" select="umbraco.library:Split($categories, ',')" />
                  <xsl:variable name="currentEventCategories" select="Exslt.ExsltStrings:lowercase(data[@alias = $eventCategories])"/>
                  <xsl:for-each select="$searchvalues/value">
                      <xsl:if test="contains($currentEventCategories, .)">
                          <xsl:value-of select="1"/>
                      </xsl:if>
                  </xsl:for-each>
              </xsl:when>
                  <xsl:otherwise>
                      <xsl:value-of select="1"/>
                  </xsl:otherwise>
          </xsl:choose>
          </xsl:variable>
    
          <xsl:if test="position() &gt; $itemsPerPage * $currentPageNumber - $itemsPerPage and position() &lt; $itemsPerPage * $currentPageNumber + 1 and $isEventPartOfCategory &gt;= 1">
          <!-- Create Article Heading -->

    This also works. The problem is that I use the count on the page and it currently counts for all of the items, and then filters out the categories that don't work. Ie. The count may be 10 articles while the actual page may only display 4 articles.

    Can anyone think of a way to merge these together so that the count and in term the select statement on the apply-template only display the items that meet the criteria of template, date, and categories?

    Thanks.

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Jun 16, 2010 @ 18:53
    Peter Duncanson
    0

    You have a lot going on in all those xpaths! Not 100% sure what you are after but I suggest you break it all down into something a bit more managable by creating various temp variables for each of your steps. Something like this (not tested and its late in the day!) :

    <!-- Passed in variables -->
    <xsl:variable name="chosenCategories" select="'fishing,cars,travel'" />
    
    <!-- All your nodes laid out and nice and readable and more importantly DRY -->
    <xsl:variable name="availableEvents" select="$currentNode/descendant::node//node [@nodeTypeAlias = $itemTemplate]" />
    <xsl:variable name="inDateEvents" select="$availableEvents[umbraco.library:FormatDateTime(data [@alias=$itemStartDateTime], $requestedMonthYearFormat) = $formattedRequestDate]]" />
    <xsl:variable name="inDateEventsCount" select="count( $inDateEvents/node )" />
    <xsl:variable name="inDateEventsForThisCategory" select="$inDateEvents/node[contains( $chosenCategories, data[@alias = $eventCategories] )]" />
    <xsl:variable name="inDateEventsForThisCategoryCount" select="count()" />
    
    <xsl:variable name="minPagePosition" select="$itemsPerPage * ( $currentPageNumber - $itemPerPage )" />
    <xsl:variable name="maxPagePosition" select="$itemsPerPage * ( $currentPageNumber + 1 )" />
    
    
     <xsl:choose>
        <xsl:when test="$inDateEventsForThisCategoryCount = 0">
            <!-- no events -->
        </xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$inDateEventsForThisCategory/node">
                <xsl:sort order="ascending" select="data[@alias = $itemStartDateTime]" data-type="text"/>
                <xsl:call-template name="RenderEvent" />
            </xsl:for-each>
            <img src="../images/level2/divider_dotted_short.gif" width="416" height="10" align="middle" class="divdot" />
            [<a href="#top" class="backToTop">Back to top</a>] 
            <br />
        </xsl:otherwise>
    </xsl:choose>

    You might have to do some msxml:node-set() wrapping around some of your variables and I'm not 100% sure what you are doing with your paging but you can see I made a start on clearing up that logic too, makes the code so much more readable and debuggable.

    Give me some more info on how you want to use it and I might be able to narrow it down for you but should give you some clues if you are asking for what I think you are asking for?

    Cheers

    Pete

     

Please Sign in or register to post replies

Write your reply to:

Draft