Copied to clipboard

Flag this post as spam?

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


  • North Krimsly 59 posts 80 karma points
    Oct 12, 2010 @ 19:00
    North Krimsly
    0

    Using getMedia in 4.5 in Xslt

    Greetings all,

    I'm having trouble using getMedia in 4.5. I have a content document "Home" which has child documents with alias "HomeSlideShowItem". Each child document has a "slideImage" which is a media picker.  I want to loop through each child document and show the slide as an image. So, the content tree looks like:

    Home

    ---Slide One (has a property alias "slideImage" of type Media Picker)

    ---Slide Two (has a property alias "slideImage" of type Media Picker)

    And the umbraco.config xml file looks like the following.  How can I access each slideImage within the child documents and display it as an image, in xslt and using getMedia? The output should look something like:

    <img src="url to slide one" alt="" />
    <img src="url to slide two" alt="" />

     

    <root id="-1"><home id="1065" parentID="-1" level="1" writerID="0" creatorID="0" nodeType="1051" template="1047" sortOrder="2" createDate="2010-09-17T14:59:21" updateDate="2010-09-18T20:36:17" nodeName="home" urlName="home" writerName="Administrator" creatorName="Administrator" path="-1,1065" isDoc="">
    [...data contained in the home page document is here...]
    <HomeSlideShowItem id="1074" parentID="1065" level="2" writerID="0" creatorID="0" nodeType="1073" template="0" sortOrder="1" createDate="2010-10-09T20:02:33" updateDate="2010-10-09T20:02:49" nodeName="Slide One" urlName="myurlone" writerName="Administrator" creatorName="Administrator" path="-1,1065,1074" isDoc="">
    <slideImage>1054</slideImage>
    </HomeSlideShowItem>
    <HomeSlideShowItem id="1075" parentID="1065" level="2" writerID="0" creatorID="0" nodeType="1073" template="0" sortOrder="2" createDate="2010-10-09T20:03:11" updateDate="2010-10-09T20:03:40" nodeName="Slide Two" urlName="myurltwo" writerName="Administrator" creatorName="Administrator" path="-1,1065,1075" isDoc="">
    <slideImage>1056</slideImage>
    </HomeSlideShowItem>
    </home>
    </root>

    Thanks for your suggestions!

    -NorthK

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 12, 2010 @ 21:05
    Chriztian Steinmeier
    0

    Hi NorthK,

    The real XSLT way to do this s to create a template slideImage element that fetches the mediaNode and outputs the image, then in your main template, just tell the processor to apply templates to the slideImage elements that has been filled:

    <xsl:template match="/">
        <xsl:apply-templates select="$currentPage/HomeSlideShowItem/slideImage[number(.)]" />
    </xsl:template>
    
    <xsl:template match="slideImage">
        <!-- Fetch media -->
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(., false())" />
        <!-- Create output -->
        <img src="{$mediaNode/umbracoFile}" alt="" />
    </xsl:template>

    /Chriztian

     

  • North Krimsly 59 posts 80 karma points
    Oct 12, 2010 @ 21:32
    North Krimsly
    0

    Greetings Chriztian,

    Thanks very much for your sample code.  Unfortunately, I don't get any results returned by the XSLT script when I replaced my code with yours.  I'm guessing nothing is being matched.  I also do not get any matches when I use "$currentPage/HomeSlideShowItem".  I only get results when I use "$currentPage/*" but of course this gives me more than I want.  Is there some change I should make to your sample code that I'm not aware of? How might I debug this?

    Thanks,

    NorthK

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 12, 2010 @ 22:05
    Chriztian Steinmeier
    0

    Hi again,

    If you're running this macro with "home" as $currentPage, you should indeed get something out, it looks like you're maybe on on of the HomeSlideShowItem pages - but fear not; we'll try to bulletproof it.

    if you change the apply-templates instruction in the root template ("/") to this, you should hit the right noeds, no matter where you are (providing the layout you described above):

    <xsl:apply-templates select="$currentPage/ancestor-or-self::home/HomeSlideShowItem/slideImage[number(.)]" />

    /Chriztian 

  • North Krimsly 59 posts 80 karma points
    Oct 12, 2010 @ 23:58
    North Krimsly
    0

    Hi Chriztian,

    I think there was an additional problem with my installation-- it didn't make sense that I wasn't getting any template matches on the Home page with very simple xpath queries, so I deleted my home page content node and rebuilt it. I can't quite explain why this worked but after that simple xpath queries worked.  I then used your code along with my own and some code from other forum posts to get the following working code.

    Thanks again for your help!

    -NorthK

     

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <xsl:for-each select="$currentPage/HomeSlideShowItem">

    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(number(slideImage), 0)"/>

    <img src="{$mediaNode/umbracoFile}" width="{$mediaNode/umbracoWidth}" height="{$mediaNode/umbracoHeight}" alt="Web design work sample" />

    </xsl:for-each>

    </xsl:template>

     

Please Sign in or register to post replies

Write your reply to:

Draft