Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    Jan 10, 2012 @ 12:59
    Gordon Saxby
    0

    Output nodes in blocks of LI's

    I have a set of nodes and I need to output them in blocks of 3 wrapped in UL tags.

    E.g.

    UL
       LI
          Node details 1
       /LI
       (3 of the above)
    /UL

    UL
       LI
          Node Details 4
          (3 of the above)
       ?LI
    /UL

    and so on

    I am not sure how to conditionally output the UL and /UL tags without causing errors!?

     

  • Gordon Saxby 1461 posts 1883 karma points
    Jan 10, 2012 @ 13:04
    Gordon Saxby
    0

    Umm, I found this "solution" but it just seems wrong!!?

              <xsl:if test="((position() - 1) mod 3) = 0">
                <xsl:text disable-output-escaping="yes">&lt;ul class="case-study-row"&gt;</xsl:text>
              </xsl:if>

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jan 10, 2012 @ 13:13
    Chriztian Steinmeier
    0

    Hi Gordon,

    Though it "works" - it *is* wrong yes :-)

    Check my answer here for a better way to do this:

    /Chriztian

     

  • Gordon Saxby 1461 posts 1883 karma points
    Jan 10, 2012 @ 13:26
    Gordon Saxby
    0

    Thanks Chriztian - your solution looks great ... although I'm not sure the code is displaying correctly? E.g. is "-templates" meant to be "xsl:apply-templates"?

    Also, would it be easy to alter it to work for 3 nodes at a time?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jan 10, 2012 @ 13:36
    Chriztian Steinmeier
    0

    Hi Gordon,

    Sounds odd??? Displays fine here - may be a browser issue then.

    I created a more generic example here: https://gist.github.com/1293712

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jan 10, 2012 @ 13:46
    Chriztian Steinmeier
    1

    - and here's an Umbraco-adapted version of that Gist:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
    
        <!-- Select the nodes to output here -->
        <xsl:variable name="nodes" select="$currentPage/*[@isDoc]" />
    
        <xsl:variable name="groupSize" select="3" />
    
        <xsl:template match="/">
            <!-- Apply the "group" mode template to the first element of every group -->
            <xsl:apply-templates select="$nodes[(position() mod $groupSize) = 1]" mode="group" />
        </xsl:template>
    
        <!-- Template for 1st item in every group -->
        <xsl:template match="*" mode="group">
            <ul>
                <!-- Apply the "item" mode template to all elements in the group -->
                <xsl:apply-templates select=". | following-sibling::*[@isDoc][position() &lt; $groupSize]" mode="item" />
            </ul>
        </xsl:template>
    
        <!-- Template for every item -->
        <xsl:template match="*" mode="item">
            <li>
                <xsl:value-of select="." />
            </li>
        </xsl:template>
    
    </xsl:stylesheet>

    If this doesn't display right in your browser we need to ping the Umbraco team about it...

    /Chriztian

  • Gordon Saxby 1461 posts 1883 karma points
    Jan 10, 2012 @ 14:05
    Gordon Saxby
    0

    Thanks Chriztian - that is / you are awesome!!! :-) Works a treat!

     

Please Sign in or register to post replies

Write your reply to:

Draft