Copied to clipboard

Flag this post as spam?

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


  • MartinB 411 posts 512 karma points
    Aug 02, 2012 @ 19:50
    MartinB
    0

    For-each insert 'external' item in loop?

    Hi there

    Let's say that i've got 4 products nodes that i traverse and then present on my frontpage.

    After product number 2 in the for-each loop i want to insert a field containing a newsletter signup. Can i do that?

    Example (pretend these are container divs):

    [product 1]    [product 2]
    
    [news letter signup field]
    [product 3]   [product 4]

    Is this possible? Could it be achieved by checking the position() parameter and still get to product 3 and 4??

    I know there are some 'boring' workarounds to this, but i want to create a product template on which i can check "showOnFrontpage" and then have it displayed like this (always 4 products).

    Any help or hints are much appreciated.

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • Dan 1285 posts 3917 karma points c-trib
    Aug 02, 2012 @ 20:12
    Dan
    2

    Hi Martin,

    You could indeed do that by checking the position.  Something roughly along these lines:

    <xsl:for-each select="your-XPath-to-select-your-products">
    <div>Your product code...</div>
    <xsl:if select="position() = 2">
    <div>Your newsletter sign-up code...</div>
    </xsl:if>
    </xsl:for-each> 

    If there's some chunky logic in either your product or newsletter it might be better to use XSLT templates, but hopefully this simple example points you in the right direction...

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 02, 2012 @ 20:26
    Chriztian Steinmeier
    2

    Hi Martin,

    I don't know about any "boring" workarounds, but it shouldn't be too hard to accomplish - you can easily do it inside the for-each, e.g.:

    <xsl:for-each select="$products">
        <div class="product">
            <!-- Do something -->
        </div>
    
        <xsl:if test="position() = 2">
            <div class="newsletter-stuff">
                <!-- Do something -->
            </div>
        </xsl:if>
    </xsl:for-each>

    Personally, I prefer using match templates for everything that looks like a "unit" - so I'd extract the Product stuff into a separate template and do something like this:

    <xsl:template match="/">
        <xsl:apply-templates select="$product[position() &lt;= 2]" />
    
        <div class="newsletter-stuff">
            <!-- Do something -->
        </div>
    
        <xsl:apply-templates select="$product[position() &gt;= 3]" />
    </xsl:template>
    
    <xsl:template match="Product">
        <div class="product">
            <!-- Do something -->
        </div>
    </xsl:template>
    

    /Chriztian 

  • MartinB 411 posts 512 karma points
    Aug 02, 2012 @ 20:36
    MartinB
    0

    Doh, it really was that simple.. and here i thought i should ask before trying it and potentially wasting numerous hours.....haha

    Thank you both for very usefull and swift replies!

    @Chriztian

    By 'boring' i meant something along the lines of making 4 individual umbraco items in a new tab on the frontpage document, placing them independently of eachother with css and then adding the product intros and images in each umbraco item.

    The above is much more 'fun' (helps me learn) and automated, when the data is taken from a product document template with some nifty xslt to handle the frontpage product loop :-)

    Saves me from entering some data twice or in two different places.

  • MartinB 411 posts 512 karma points
    Aug 02, 2012 @ 21:39
    MartinB
    0

    Just wanted to let you know that both your suggestions work like a charm. 

    Since i haven't worked much with applying templates before it took me a little while to figure out that i should define my product variable before the template calls in Chriztians example. Great stuff :-)

  • MartinB 411 posts 512 karma points
    Aug 04, 2012 @ 00:12
    MartinB
    0

    One question Chriztian.

    Is the for-each an implicit part of the apply-template function? 

    I was wondering how your example (as I am currently using) is traversing the nodes when the for-each is not defined in the code.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 04, 2012 @ 00:51
    Chriztian Steinmeier
    1

    Hi Martin,

    Yeah, that's the huge misconception about XSLT: It's not really a programming language, so it doesn't work like you're used to :-)

    I did a presentation on that at Codegarden ‘10 - slides and a link to the video here: http://pimpmyxslt.com/presentations/2010/xslt-beyond-for-each/

    But basically, XSLT works by defining templates for the elements you need to process, and then you use the apply-templates instruction to select those elements and have the processor go through them, using those templates to generate the output tree. The for-each statement is really just a shortcut to having an apply-templates instruction with a single embedded template.

    When you learn how XSLT really works, you'll rarely be using for-each, though it's still needed in some specific scenarios... 

    /Chriztian

  • MartinB 411 posts 512 karma points
    Aug 05, 2012 @ 20:11
    MartinB
    0

    Ah, nice.

    Thank you very much for the short intro and the reference. Great stuff!

Please Sign in or register to post replies

Write your reply to:

Draft