Copied to clipboard

Flag this post as spam?

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


  • David Ferguson 15 posts 35 karma points
    Aug 09, 2010 @ 18:58
    David Ferguson
    0

    Looping the first x records

    I have a website that displays News items on the homepage. The user can select which articles to display on the homepage with use of the true/false field assigned to each article.

    I only want to display the first 4 articles that have this flag set to true.

    I am looping using a condition which checks the position() and when it reaches 4 it breaks the loop. However, if I disable the first news item in the list, only 3 items are displayed on the home page. I assume that the position is counting the first news item and therefore when the position gets to 4 it has actually only found 3 which are active.

    My code;

    <xsl:for-each select="$currentPage/ancestor-or-self::node//node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='active']) = '1' and string(data [@alias='teaseHomepage'])='1' and position()!=4]">

    <div class="newsHomeHeader">

    <a href="{umbraco.library:NiceUrl(@id)}"><strong><xsl:value-of select="substring(data [@alias = 'pageTitle'],1,40)"/></strong></a>

    </div>

     

    <div class="newsHomeStory">

    <xsl:value-of select="substring(data [@alias = 'summary'],1,50)"/>...

    </div>

    </xsl:for-each>

     

    So how do I display 4 items that have the tease flag set to true?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Aug 09, 2010 @ 19:05
    Tom Fulton
    1

    Just for kicks, try:

    <xsl:for-each select="$currentPage/ancestor-or-self::node//node 
    [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='active']) =
    '1' and string(data [@alias='teaseHomepage'])='1'][position()!=4]">

    I seem to recall a similar issue fixed by putting the position() condition in it's own bracket set.  Not entirely sure though :)

    Also I would think you should probably use something like [position &lt; 5], otherwise (I would think) it would keep going after the 4th and just not include the 4th.

  • Donald St. Martin 83 posts 128 karma points
    Aug 09, 2010 @ 20:09
    Donald St. Martin
    0

    Here is my for-each loop that I use to display the 7 latest items from an RSS feed:

    <xsl:for-each select="$y/rss/channel/item">
                <xsl:if test="position() &lt; 8">
                    <li class="feeditem">
                        <xsl:variable name="href" select="link" />
                        <xsl:variable name="title" select="title" />
                                <a href="{$href}" alt="{$title}" title="{$title}" target="_blank">
                                    <xsl:value-of select="title" disable-output-escaping="yes"/>
                        </a>
                    </li>
                </xsl:if>
            </xsl:for-each>

    Just add your flag to the for-each loop just like you would add the umbracoNaviHide.

    Hope this helps,

    --
    Donald

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Aug 09, 2010 @ 22:08
    Chriztian Steinmeier
    0

    Hi David,

    Tom's right - moving the position() criterion to a separate predicate (square brackets ~= "filter") makes it operate on the result of the previous filter instead of being part of that filter. So the first part selects all the candidates ($documentTypeAlias, $active and $teaseHomePage) which creates kind of a temporary node-set, which you apply the position filter to afterwards to get only the 4 you want.

    And yes, you will also need to use [position() &lt; 5] to get the first 4...

    /Chriztian

  • David Ferguson 15 posts 35 karma points
    Aug 10, 2010 @ 01:49
    David Ferguson
    0

    Thanks guys, your posts were right on the money.. I love this forum!!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies