Copied to clipboard

Flag this post as spam?

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


  • Devin 87 posts 251 karma points
    May 21, 2014 @ 15:25
    Devin
    0

    For-each - image

    I have 4 Media Picker items in a document type (SlideImage1, SlideImage2, SlideImage3 and SlideImage4). I would like to be able to show every image out of those 4, so if 1 exists, only show 1, if 2 exists, show 2 etc. If 1, 2, 3, 4 exists then show all of them.

    What's the easiest way to check if 2, 3 or 4 exists? About future proofing, is there a way to check if another alias is added (a 5th item) and to automatically add that in?

    Thanks

     

  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    May 21, 2014 @ 22:54
    Chriztian Steinmeier
    100

    Hi Devin,

    This is exactly the job of match templates in XSLT:

    <xsl:template match="/">
        <section class="slides">
            <!-- Process all <SlideImageX> properties that have a value -->
            <xsl:apply-templates select="$currentPage/*[starts-with(name(), 'SlideImage')][normalize-space()]" />
        </section>
    </xsl:template>
    
    <!-- Template for a <SlideImageX> property -->
    <xsl:template match="*[starts-with(name(), 'SlideImage')]">
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(., false())" />
        <xsl:if test="not($mediaNode[error])">
            <img src="{$mediaNode/umbracoFile}" />
        </xsl:if>
    </xsl:template>
    

    Hope that gets you going!

    /Chriztian

  • Devin 87 posts 251 karma points
    May 22, 2014 @ 11:00
    Devin
    0

    Hi Chriztian,

    That's great thanks. I know you can only pass one value here though so what about SlideImage2, 3 and 4?

        <xsl:variablename="mediaNode"select="umbraco.library:GetMedia(SlideImage1, false())"/>
  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    May 22, 2014 @ 11:12
    Chriztian Steinmeier
    0

    Hi Devin,

    You didn't try it, did you? :)

    The apply-templates line takes care of selecting all properties with a name starting with 'SlideImage' - the template below does the rendering of each of those. Future-proof already :-)

    /Chriztian

  • Devin 87 posts 251 karma points
    May 22, 2014 @ 11:25
    Devin
    0

    Hi :)

    Apologies,I should've posted my code as I am using a variable $sharedNode which is grabbing the images from another document. My old code is as follows below, so I guess I also need to call the $sharedNode variable, just not sure on the syntax (not great with XPath).

    How do I add the sharedNode variable into this example?

    <xsl:variablename="mediaNode"select="umbraco.library:GetMedia(., false())"/>

    Thanks for your help, makes complete sense though.

  • Devin 87 posts 251 karma points
    May 22, 2014 @ 11:30
    Devin
    0

    It's ok figured it out. When applying the template, I just had to change $currentPage to $sharedNode.

    Thanks so much Chriztian :)

  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    May 22, 2014 @ 11:40
    Chriztian Steinmeier
    0

    Awesome - you're welcome :-)

Please Sign in or register to post replies

Write your reply to:

Draft