Copied to clipboard

Flag this post as spam?

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


  • Hundebol 167 posts 314 karma points
    Aug 21, 2009 @ 16:15
    Hundebol
    0

    Getting image from Media library or another page

    HI guys,

    I have the following xslt:

    <xsl:for-each select="$currentPage/self::node [string(data [@alias='umbracoNaviHide']) != '1']">
    <xsl:choose>
            <xsl:when test="string(data[@alias = 'topPicture']) != ''">
        <xsl:value-of select="data [@alias = 'topPicture']"/>
            </xsl:when>
            <xsl:otherwise>
            </xsl:otherwise>
          </xsl:choose>
    </xsl:for-each>

    What I want to archieve is this: If there is no "topPicture" chosen on the specific site, it should show an image from either the Media library or the "Home"-page. This is in my logic going in the "otherwise" - But how to write?

    I have tried with some different GetMedia without much luck. Hope someone can point me in the right direction.

    Best regards,
    Brian

  • Masood Afzal 176 posts 522 karma points
    Aug 21, 2009 @ 16:45
    Masood Afzal
    1

    Try something like this:

    <xsl:element name="img">
        <xsl:attribute name="src">
             <xsl:value-of select="umbraco.library:GetMedia($thisPage/data[@alias='topPicture'], 'false')/data [@alias = 'umbracoFile']"/>
        </xsl:attribute>
        <xsl:attribute name="alt">
             <xsl:value-of select="string('alt text')"/>
        </xsl:attribute>
    </xsl:element>
  • Chris Koiak 700 posts 2626 karma points
    Aug 21, 2009 @ 16:48
    Chris Koiak
    1

    Hi,

    Try something like


    <xsl:choose>
           
    <xsl:when test="string(data[@alias = 'topPicture']) != ''">
       
    <xsl:value-of select="data [@alias = 'topPicture']"/>
           
    </xsl:when>
           
    <xsl:otherwise>
    <xsl:variable name="parentImage" select="$currentPage/ancestor-or-self::node[@level=1]/data [@alias = 'topPictureMediaId']"/>
    <xsl:if test="string-length($parentImage) > 0"/>
    <img src="{umbraco.library:GetMedia($parentImage, 'false')/data [@alias = 'umbracoFile']}"/>
    </xsl:if
    >
            </xsl:otherwise>
    </xsl:choose>

    I don't see the need for the for-each?

     

    There are more properties available in the xml returned from GetMedia. Try the code below to see more options

    <textarea cols="30" rows="30">
    <xsl:copy-of select="umbraco.library:GetMedia(99999, 'false')"/>
    </textarea>
  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 21, 2009 @ 16:58
    Dirk De Grave
    0

    btw, if your image is of upload data type rather than a media picker, then you no longer need GetMedia call as the path to the file will exist as property of the document you're processing

     

    cheers,

    /dirk

  • Hundebol 167 posts 314 karma points
    Aug 24, 2009 @ 09:27
    Hundebol
    0

    Thank you all for your suggestions.

    #Masood: I could not get you suggestion to work at all. It tells me there is an error in the value-of-select.

    #Chris Koiak: Your's work just as well or just as bad as my own version. But I need the for-each - elsewise nothing shows. But your suggestion also shows an error in the if-part. So things have not really helped that much.

    #Dirk De Grave: The media type is an upload type right now, but it WILL be a media picker later.

    Hope to hear back, as it is probably me who cannot figure out how to tweak the code right!

    best regards,
    Brian

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 24, 2009 @ 09:40
    Dirk De Grave
    0

    Brian,

    Let's review your initial request:

    you want your 'otherwise' to be populated with an image from either the media gallery or the 'home' page, but you haven't specify how? so, it will be quite difficult for us to guess what you're after.

    Masood's example is ok, but you've probably not specified the variable 'thisImage' from the code snippet, resulting in an error on save.

    Don't understand the reason behind the need for a for-each either, why do you need this, can you tell us a bit more about the context (other than 'I want to display an image'). Any specific reason for the for-each loop.

    And, if you will be using a media picker instead of a upload later on, I'd suggest to do it now, otherwise you'll have to change your code once again, sounds like double work!

     

    So, let us know what exactly you want to display, how you'd like to make to behave (especially the 'otherwise' part) and do tell us a bit more about the use of for-each and the context!

     

    Cheers,

    /Dirk

  • Hundebol 167 posts 314 karma points
    Aug 24, 2009 @ 10:14
    Hundebol
    0

    Hi Dirk,

    Okay, let's take it from the top. Forget all about my own code and all that.

    What i want to archieve
    -> On each of my pages, the should be a Top Picture (Banner). The user can specify a special picture for a special page. But if the user does not specify a toppicture, a "Default picture" should show. The data type is Media Picker now, as it shall be.

    So, if there is no specific image choosen for a specific subpage, the "default image" from the Media Library shall be shown.

    1. If there is chosen a picture for the specific subpage - show that!
    2. If there is NOT chosen a picture - show the "Default picture" from the Media Library

    ---
    I have no reason for using the for-each. I have no reason for using the Choose either - I still learning Xslt, so it means a lot of trial-and-error. And the for-each thing in my code worked - so I just thought it should be there.

    My Media picker have the alias "topPicture" - as you might know. And my Media Library item have the alias "toppicture" (ALL small letters) and the ID of 1159.

    in Masood's code i changed the

    @alias = 'umbracoFile'

    to "toppicture" instead of umbracoFile. But maybe that is wrong? In that case, please be so kind to tell me what I should really change, as I probably am to dumb at Xslt to understand for my self ;)

    best regards,
    Brian

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 24, 2009 @ 10:30
    Dirk De Grave
    0

    Brian,

    <img>
    <xsl:attribute name="src">
    <xsl:choose>
    <xsl:when test="string(data[@alias = 'topPicture']) != ''">
    <xsl:value-of select="umbraco.library:GetMedia(data [@alias = 'topPicture'], 'false')/data [@alias = 'umbracoFile']"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="umbraco.library:GetMedia(1159, 'false')/data [@alias = 'umbracoFile']"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:attribute>

    Above code snippet is just putting the pieces together from what Masood and Chris have been suggesting before£.

    First, there's a test to verify whether an image has been choosen for the current page. If the string(..) is not empty, than indeed a picture has been choosen. As you're now using a media picker, this is just an id of the media item, therefore we need a call to GetMedia() to get the info about the picture. You're feeding the GetMedia() with 2 params, first being the id of the media item, and a boolean param (just tells the function to not perform a recursive find)

    GetMedia() again returns an xml fragment from which you need the value of the data node which has an attribute called 'umbracoFile'. (umbracoFile is some kind of 'standard' name which is widely used in umbraco when it comes to media items)

    Your otherwise path in the code snippet has to be populated with the id of the default picture from the media library (Hover over the media item and look at the bottom left to find the id, think that's 1159 in your case)

     

    "My Media picker have the alias "topPicture" - as you might know. And my Media Library item have the alias "toppicture" (ALL small letters) and the ID of 1159."

    Aliases don't apply to media items, those have just a name (which is the name you set yourself and is visible in the tree)

     

    Agreed, xlst has a learning curve, but you'll learn to appreciate once you get to know the basics/advanced features.

     

    Cheers,

    Dirk

     

  • Hundebol 167 posts 314 karma points
    Aug 24, 2009 @ 11:24
    Hundebol
    0

    Dirk,

    Thank you for your really GREAT help! I deeply appreciate it.

    The code you wrote works almost - It displays the image from the Media Library when no other pictures are chosen.
    BUT, it also shows the image from the Media Library, when there infact is chosen another image on the specific page.

    I surely can't figure it out!

    best regards,
    Brian

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Aug 24, 2009 @ 11:35
    Peter Dijksterhuis
    0

    I think you need to alter this bit: 

    data [@alias = 'topPicture']

    as you said that you had an alias with all lowercase.

    HTH,

    Peter

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 24, 2009 @ 11:41
    Dirk De Grave
    0

    Bit odd to me... the choose construct seems to work fine in one case, but not the other? Are you sure there's nothing else in the xslt which may cause it to behave so oddly? In such rare occasions, I also output some extra info to check where i'm going wrong.

    I'd add 

    **<xsl:value-of select="string(data[@alias = 'topPicture'])"/>** 

    just before the <img> tag

     

    If you do so, what output do you get?

     

    Cheers,

    /Dirk

     

     

  • Hundebol 167 posts 314 karma points
    Aug 24, 2009 @ 11:46
    Hundebol
    0

    When I add that line, the output is four stars like this: ****

    I believe it is the stars around the code, that's outputting?

    best regards,
    Brian

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 24, 2009 @ 11:53
    Dirk De Grave
    0

    which is what you would get if no picture has been choosen for that specific page, and in which case the default pic should be shown, right? Sounds as the correct behaviour. And what do you get when using this on a page for which a pic has been choosen?

  • Hundebol 167 posts 314 karma points
    Aug 24, 2009 @ 12:10
    Hundebol
    0

    The same! :)

    It does exactly the same, outputs the 4 stars.

    So this part:

    <xsl:when test="string(data[@alias = 'topPicture']) != ''">
    <xsl:value-of select="umbraco.library:GetMedia(data [@alias = 'topPicture'], 'false')/data [@alias = 'umbracoFile']"/>
    </xsl:when>

    Is somehow not working correctly. Somehow, it just goes right to the otherwise no matter what. The complete code is here:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template match="/">
    **<xsl:value-of select="string(data[@alias = 'topPicture'])"/>**
    <img>
    <xsl:attribute name="src">
    <xsl:choose>
    <xsl:when test="string(data[@alias = 'topPicture']) != ''">
    <xsl:value-of select="umbraco.library:GetMedia(data [@alias = 'topPicture'], 'false')/data [@alias = 'umbracoFile']"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="umbraco.library:GetMedia(1286, 'false')/data [@alias = 'umbracoFile']"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:attribute>
    </img>
    </xsl:template>
    </xsl:stylesheet>

    And i should not do anything when inserting the macro, should I? Just a plain insert Macro should do.

    best regards,
    Brian

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 24, 2009 @ 12:34
    Dirk De Grave
    0

    Brian,

    Just change a single line of code:

    <xsl:when test="string($currentPage/data[@alias = 'topPicture']) != ''">

    instead of

    <xsl:when test="string(data[@alias = 'topPicture']) != ''">

    which only make sense in a for-each loop (as then it refers to the current item being processed in the loop)

     

    Cheers,

    /Dirk

  • Hundebol 167 posts 314 karma points
    Aug 24, 2009 @ 12:44
    Hundebol
    0

    Dirk,

    and then the ouput is: Error parsing XSLT file: \xslt\TopPicture.xslt

    This soon turns out to be a soap. Maybe i should try other alternatives?

    best regards,
    Brian

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 24, 2009 @ 12:54
    Dirk De Grave
    0

    Append ?umbdebugshowtrace=true to url to find out about the error! (it will be marked in red in the trace output)

  • Hundebol 167 posts 314 karma points
    Aug 24, 2009 @ 13:04
    Hundebol
    0

    Nothing shows in red!

    The only thing I could read out of this, with regards to the macro is this:

    umbracoMacro    Macro loaded from cache (ID: 67, Top Picture)   
    renderMacro    Rendering started (macro: Top Picture, type: 1, cacheRate: 0)

    But nothing in red, that's for sure.

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Aug 24, 2009 @ 13:04
    Peter Dijksterhuis
    0

    I think you need to append the $currentPage to the second one as well:

    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias = 'topPicture'], 'false')/data [@alias = 'umbracoFile']"/>

    HTH,

    Peter

     

     

  • Hundebol 167 posts 314 karma points
    Aug 24, 2009 @ 14:58
    Hundebol
    0

    Hi Peter,

    That is not possible, the Xslt tells there is an error. And it should not be the same, as it should do two different things.

    But thanks for your suggestion - I think i have to find another possible way of doing this as i really does not work that well.

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Aug 24, 2009 @ 15:04
    Peter Dijksterhuis
    100

    No, you misunderstood me.

    data[@alias = 'topPicture'] occurs twice in the script. Both should be $currentPage/data[@alias = 'topPicture']

    <xsl:when test="string($currentPage/data[@alias = 'topPicture']) != ''">
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias = 'topPicture'], 'false')/data [@alias = 'umbracoFile']"/>
    </xsl:when>

  • Hundebol 167 posts 314 karma points
    Aug 25, 2009 @ 08:50
    Hundebol
    0

    Hi Peter

    I sure did misunderstand you, Peter!

    I think I was way over the top of my head at the moment, so i did not really manage to "read and understand" what you wrote.

    But, you were right - it works like a charm now!

    The full code is here for future use:

    <img>
    <xsl:attribute name="src">
    <xsl:choose>
    <xsl:when test="string($currentPage/data[@alias = 'topPicture']) != ''">
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias = 'topPicture'], 'false')/data [@alias = 'umbracoFile']"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="umbraco.library:GetMedia(1286, 'false')/data [@alias = 'umbracoFile']"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:attribute>
    </img>

    Thanks to Dirk for his great help, but I set Peter's last post as the solution when i can't set this post (with the full code) as the solution.

Please Sign in or register to post replies

Write your reply to:

Draft