Copied to clipboard

Flag this post as spam?

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


  • Gonçalo Assunção 39 posts 68 karma points
    Dec 16, 2014 @ 19:16
    Gonçalo Assunção
    0

    Creating some kind of "for-loop"

    Hello guys,

     

    I'm still learning my way through Umbraco/XSLT.
    I created a xslt macro, for a form, which has some kind of date pickers made with select tags from HTML.
    Now I want to create some kind of loop that checks all the (option) from my select, and if the (option) matches the current month, that (option) is the "selected" one.

    So, what I am aiming for here is:

    - get the current month (Already achieved this );
    - loop through all the 12 month (option);
    - If (option value="currentMonth"), then selected="selected"

     

    Thank you in advance!

  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    Dec 16, 2014 @ 20:25
    Chriztian Steinmeier
    1

    Hi Gonçalo,

    Here's how I'd probably start:

    <!-- Construct a "months" XML variable that we can use XPath on -->
    <xsl:variable name="monthsProxy">
        <month id="01">January</month>
        <month id="02">February</month>
        <month id="03">March</month>
        <month id="04">April</month>
        <month id="05">May</month>
        <month id="06">June</month>
        <month id="07">July</month>
        <month id="08">August</month>
        <month id="09">September</month>
        <month id="10">October</month>
        <month id="11">November</month>
        <month id="12">December</month>
    </xsl:variable>
    <xsl:variable name="months" select="msxml:node-set($monthsProxy)" />
    
    <!-- Get "today" as an XMLDate (e.g.: 2014-12-16T20:20:00) -->
    <xsl:variable name="today" select="umbraco.library:CurrentDate()" />
    <!-- Extract the month  -->
    <xsl:variable name="currentMonth" select="substring($today, 6, 2)" />
    
    <xsl:template match="/">
        <select name="month">
            <!-- Create an `<option>` tag for every month... -->
            <xsl:for-each select="$months/month">
                <option value="{@id}">
                    <!-- Add the selected attribute if this is the current month -->
                    <xsl:if test="@id = $currentMonth"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>
                    <xsl:value-of select="." />
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>
    

    Hope that gets you started too,

    /Chriztian

  • Gonçalo Assunção 39 posts 68 karma points
    Dec 17, 2014 @ 11:30
    Gonçalo Assunção
    0

    That worked well!

    Thank you Chriztian 

Please Sign in or register to post replies

Write your reply to:

Draft