Copied to clipboard

Flag this post as spam?

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


  • ian 35 posts 135 karma points
    May 07, 2013 @ 17:44
    ian
    0

    display media picker images in search results

    Hi,

    I am trying to display a product image in my XSLT search results page. I have a productInfo document type with a media picker alias mainProductImage to select the product image from the media library. All is fine when I view the products I just cant seem to set the HTML src on the results llisting page. I have tried adding the GetMedia function syntax under the OPTIONAL: include any additional information etc... remark and I keep getting error parsing XSLT file when the page loads.

    thank you in advance

     

    ian

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    May 08, 2013 @ 09:06
    Douglas Robar
    0

    Hi, Ian,

    Sounds like you're in the correct place in the XSLTsearch.xslt file to put in the extra info. Can you show us what you've added in the OPTIONAL section? Also, if you get an error, what's the error?

    cheers,
    doug. 

  • ian 35 posts 135 karma points
    May 08, 2013 @ 09:49
    ian
    0

    hi doug,

     

    thanks for the reply. below are the few ways ive tried it.. the error I get is error parsing xslt file when i load the results page.

    thanks

    ian

                    <!-- OPTIONAL: include any additional information regarding the search result you want to display, such as createDate, updateDate, author, etc. -->                
    
                    <!--***this version below produces error parsing xslt file *****-->
                    <!-- <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/mainProductImage, 0)" /> <xsl:if test="$media"> <img src="{$media/umbracoFile}" alt="{$media/altText}" /> </xsl:if> -->
    
                    <!-- **** this version sets the src as the node ID ***** <img style="width:125px"> <xsl:attribute name="src"> <xsl:value-of select="mainProductImage"/> </xsl:attribute> </img> -->
    
    
                    <!-- ***** this version also displays error parsing xslt file ******-->
                    <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/mainProductImage, 0)" />
    
                     <xsl:if test="$media">
                            <xsl:variable name="url" select="$media/umbracoFile" />
                            <img src="{$url}" />
                     </xsl:if>
    
    
  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    May 08, 2013 @ 10:24
    Douglas Robar
    0

    Hi, Ian,

    Ahhh, the old 'getmedia errors on save' problem! It causes a lot of unnecessary head scratching. 

    When Umbraco saves and xslt file it compiles it and runs it against the first node in the content tree as a sanity check. This is usually just fine but sometimes it will give an error because the logic of your xslt doesn't makes sense in the context of the first page of the content tree (the home page of your site, typically). The logic may well make sense when the xslt is used in its proper context though, and that's when you'd want to tell Umbraco that you know better and to please save the xslt file without running the test first. To do this, tick the checkbox to skip testing.

    Usually you don't want to tick the 'skip testing' box because it's almost always correct. But not so when using the umbraco.library:GetMedia() call. If the item you're looking for isn't available on the first node of the site it will give an error. Thus, when you ask for $currentPage/mainProductImage... it isn't there when tested against the first page in the site. And GetMedia() complains with an error. 

    There are two ways around this. The quick one is to tick the 'skip testing' box, save the xslt, and see if it runs properly when used in its appropriate context on the site (the search page and results). The better way is to add an xsl:if statement around the whole GetMedia() section so that it will pass the built-in tests and also work properly on the site.

    I'd do something like this:

    <!-- only attempt to get image if the current page has the appropriate property -->
    <xsl:if test="$currentPage/mainProductImage > 0">
    <xsl:variable name="prodImage" select="umbraco.library:GetMedia($currentPage/mainProductImage, 0)" />
    <!-- ensure the referenced image has an associated file -->
    <xsl:if test="$prodImage/umbracoFile"> 
    <img src="{$prodImage/umbracoFile}" />
    </xsl:if>
    </xsl:if> 

     

    The first xsl:if will solve the problem of failing during the test when saving the xslt file. The second xsl:if is just a safety and strictly speaking isn't necessary but I like to be conservative and imagine potential problems like someone deleting a media item that shouldn't have been deleted.

     

    Hope this helps explain why you were having errors when your code was, in fact, just fine.

    cheers,
    doug. 

  • ian 35 posts 135 karma points
    May 08, 2013 @ 10:54
    ian
    0

    Hi Doug,

    I had a feeling it was getmedia falling over. I have pasted in your code(thank you), it doesn't throw an error now but it still doesn't display the images as expected. So one of the IF's must be failing. Not quite sure why.. I have a property mediapicker in my documnet type alias mainProductImage and all the images are stored in the media folder. I can view the various products fine including the images... ( i have reloaded all nodes and republished the entire site just in case but still doesn't work)

     

    ian

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    May 08, 2013 @ 11:08
    Douglas Robar
    0

    Do'h! My bad. Should have had another cup of coffee before answering :(

    You don't need the $currentPage part of the xpath statement... you aren't considering the properties on the search page itself (that's what $currentpage would be in this context). Rather, you want the properties of the node being displayed. That's already in context in the template's loop so you can simplify to:

    <!-- only attempt to get image if the current page has the appropriate property -->
    <xsl:iftest="mainProductImage > 0">
       
    <xsl:variablename="prodImage"select="umbraco.library:GetMedia(mainProductImage, 0)"/>
       
    <!-- ensure the referenced image has an associated file -->
       
    <xsl:iftest="$prodImage/umbracoFile">
           
    <imgsrc="{$prodImage/umbracoFile}"/>
       
    </xsl:if>
    </xsl:if>

    cheers,
    doug. 

  • ian 35 posts 135 karma points
    May 08, 2013 @ 11:20
    ian
    100

    Hi Doug,

    It worked!, thank you once again.

     

    Ian

Please Sign in or register to post replies

Write your reply to:

Draft