Copied to clipboard

Flag this post as spam?

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


  • Kasper Dyrvig 246 posts 379 karma points
    Sep 12, 2011 @ 13:00
    Kasper Dyrvig
    0

    Loop this code 10 times

    Hi all

    I want to create a dropdownboxs with the 10 next coming week numbers. Basically I want to repeat this code X number of times:

    <xsl:variable name="daysInFuture">P<xsl:value-of select="number(7 * position())"/>D</xsl:variable>
    <li><href="/details.aspx?fromdate={umbraco.library:Replace(umbraco.library:FormatDateTime(Exslt.ExsltDatesAndTimes:add($thisDate, 'P7D'), 'M-d-yyyy'), '-', '%2f')}">
      Week <xsl:value-of select="Exslt.ExsltDatesAndTimes:weekinyear(Exslt.ExsltDatesAndTimes:add($thisDate, $daysInFuture))"/></a></li>

    Right now I am here:

    <xsl:variable name="weeksInFuture" select="'10'"/>
    <xsl:variable name="thisWeek" select="Exslt.ExsltDatesAndTimes:weekinyear()"/>
    <xsl:variable name="thisDate" select="Exslt.ExsltDatesAndTimes:date()"/>
        
    <xsl:template match="/">
      <p><xsl:value-of select="$weeksInFuture"/><br /><xsl:value-of select="$thisWeek"/><br /><xsl:value-of select="$thisDate"/></p>
      <ul>
        <xsl:for-each select=" ??? ">
          <xsl:apply-templates />
        </xsl:for-each>
      </ul>
    </xsl:template>
        
    <xsl:template match="/">
      <xsl:variable name="daysInFuture">P<xsl:value-of select="number(7 * position())"/>D</xsl:variable>
      <li><href="/details.aspx?fromdate={umbraco.library:Replace(umbraco.library:FormatDateTime(Exslt.ExsltDatesAndTimes:add($thisDate, 'P7D'), 'M-d-yyyy'), '-', '%2f')}">
          Week <xsl:value-of select="Exslt.ExsltDatesAndTimes:weekinyear(Exslt.ExsltDatesAndTimes:add($thisDate, $daysInFuture))"/></a></li>
    </xsl:template>

    But I don't know how to provoke a loop, with having something to loop through...?

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Sep 12, 2011 @ 13:09
    Chriztian Steinmeier
    2

    Hi Kasper,

    For this, you would usually use a recursive template (one that calls itself, unless some condition is met).

    Simple example:

    <xsl:template match="/">
        <!-- Start repeating -->
        <xsl:call-template name="repeatable" />
    </xsl:template>
    
    <xsl:template name="repeatable">
        <xsl:param name="index" select="1" />
        <xsl:param name="total" select="10" />
    
        <!-- Do something -->
    
        <xsl:if test="not($index = $total)">
            <xsl:call-template name="repeatable">
                <xsl:with-param name="index" select="$index + 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    

    /Chriztian

  • Euan Rae 105 posts 134 karma points
    Sep 12, 2011 @ 13:10
    Euan Rae
    0

    Change the weeksInFuture variable like so:

    <xsl:variable name="weeksInFuture" select="'1,2,3,4,5,6,7,8,9,10'"/>

    and now the for each loop can be:

    <xsl:for-each select="Exslt.ExsltStrings:split($weeksInFuture,',')">

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Sep 12, 2011 @ 13:10
    Chriztian Steinmeier
    1

    - For a more elaborate example (and craziness), check this Gist out: https://gist.github.com/839751

    /Chriztian

  • Kasper Dyrvig 246 posts 379 karma points
    Sep 12, 2011 @ 13:36
    Kasper Dyrvig
    0

    Yeay, Chriztian! It works.

    Thanks a lot

  • 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