Copied to clipboard

Flag this post as spam?

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


  • Darryl Godden 145 posts 197 karma points
    Feb 09, 2010 @ 11:06
    Darryl Godden
    0

    Get image in XSLT from 'Media' content type

    Hi,

    I'm trying to get an image to display in my XSLT. I have created a content type with a few text fields and a media type, when I use insert XSLT value-of and select the media field, all I get is a display of a number, here is my code:

    <ul>

    <xsl:for-each select="$currentPage/ancestor-or-self::root/node [@nodeTypeAlias='WhatsOn']/node">

    <li><xsl:value-of select="data [@alias = 'TourTitle']"/><br />

    <xsl:value-of select="data [@alias = 'Media']"/>

    <span style="font-size:11px;">

    <xsl:value-of select="data [@alias = 'EventDescription']" disable-output-escaping="yes"/></span>

    </li>

    </xsl:for-each>

    </ul>

     

    Thanks.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 09, 2010 @ 11:11
    Dirk De Grave
    1

    Hi,

    You should use

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

     

    Cheers,

    /Dirk

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 09, 2010 @ 11:13
    Dirk De Grave
    3

    Here's a short explanation on the use of GetMedia

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Feb 09, 2010 @ 11:13
    Sebastiaan Janssen
    2

    Have a look at Lee Kelleher's blog post about this, it explains all of the magic! :)

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 09, 2010 @ 11:26
    Lee Kelleher
    5

    Dirk!!! Stop using that example, (it's bad practice IMHO - but I still love you), the 'false' string equates to true() in XSLT - where 0 (zero) or false() should be used.

    Then there is the assumption that GetMedia will return the data nodes.  If there isn't, then a big dirty exception is thrown... which is why I'm trying to encourage the "safer" approach - especially for XSLT beginners.

    @Darryl, here goes another plug for my blog post on "How to use umbraco.library GetMedia in XSLT", here's a snippet:

    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(data[@alias='Media'], 0)" />
    <xsl:if test="count($mediaNode/data) &gt; 0 and string($mediaNode/data[@alias='umbracoFile']) != ''">
        <img src="{$mediaNode/data[@alias='umbracoFile']}" alt="[Hey hey I'm an alt tag!]" />
    </xsl:if>

    Obviously, it's not a one-liner... but less headaches.

    Right, I'll get off my high horse now.

    Mucho amor, Lee.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 09, 2010 @ 11:28
    Lee Kelleher
    0

    Oops, was a bit slow in posting.  Thanks guys!

  • Darryl Godden 145 posts 197 karma points
    Feb 09, 2010 @ 12:01
    Darryl Godden
    0

    Thanks for your help Lee, Dirks' worked for now, but I shall look at your 'safer' method.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 09, 2010 @ 12:14
    Lee Kelleher
    0

    Hi Darryl, cool.  Dirk's answer will work for 99.9999999999% of the time with no problems!

    The "safer" approach is there to catch the "what if's" ... like if "data[@alias='Media']" is empty, or doesn't contain a numeric value? or if the media node is empty, or has been deleted? or something else that you haven't thought of (yet).

    If you know your content/data, that's great ... I've experienced too many content-editors doing crazy things lately! (especially deleting media nodes) ;-)

    Cheers, Lee.

  • Amir Khan 1282 posts 2739 karma points
    Feb 08, 2011 @ 18:09
    Amir Khan
    0

    I'm trying to implement this but am getting nothing returned, possibly because the macro is inserted in the master template and the media picker is on a document type of a template under the master?


    Not sure what's going on, any help would be greatly appreciated. I've used this code several times without issue...

     

    <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/data[@alias='bannerImage'], 0)/data" />

     
    <xsl:if test="$media">
           
    <xsl:variable name="url" select="$media [@alias = 'umbracoFile']" />
           
    <xsl:variable name="width" select="$media [@alias = 'umbracoWidth']" />
           
    <xsl:variable name="height" select="$media [@alias = 'umbracoHeight']" />
           
    <img src="{$url}" width="{$width}" height="{$height}" />
     
    </xsl:if>
  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Feb 09, 2011 @ 08:11
    Sebastiaan Janssen
    0

    Amir, are you using Umbraco 4.5 or higher with the new XML schema (which is enabled by default)? In that case this will not work indeed, have a look at Lee's blog post that covers getmedia using the new schema.

    If you're using the old XML schema, this looks fine and you should try and try something like this to see what the result of the GetMedia call is:

    <textarea>
     <xsl:copy-of select="umbraco.library:GetMedia($currentPage/data[@alias='bannerImage'], 0)" />
    </textarea>
  • Amir Khan 1282 posts 2739 karma points
    Feb 09, 2011 @ 15:30
    Amir Khan
    0

    Sebastiaan, thanks for your response. I was using 4.5.x (2 i think), with the legacy schema. After trying several other XSLTs with both the legacy and new schema I was having tons of errors, as well as errors pulling simple things like page fields into a template. I wonder if I had just messed up a config file at some point, I've never experienced a problem like that before...Regardless, I did a fresh install of Juno, left new schema enabled, and used the following (slight adaptation of Lee's code) which pulls the image from MediaPicker, alt tag from node name, and the height and width very nicely. Thanks for your help! - Amir

     

    <xsl:template match="/">
      <xsl:variable name="mediaId" select="number($currentPage/testImage)" />
      <xsl:if test="$mediaId > 0">
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
        <xsl:if test="$mediaNode/umbracoFile">
          <img src="{$mediaNode/umbracoFile}" alt="{$mediaNode/@nodeName}" height="{$mediaNode/umbracoHeight}" width="{$mediaNode/umbracoWidth}" />
        </xsl:if>
      </xsl:if>
    </xsl:template>
  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Feb 09, 2011 @ 18:28
    Sebastiaan Janssen
    0

    I suspect that you have been switching between the old and new schema before you did a reinstall?

    If you don't do the switch in the way described here, you end up with old and new schema xml mixed in the umbraco.config, which would explain your results.

  • Amir Khan 1282 posts 2739 karma points
    Feb 09, 2011 @ 20:30
    Amir Khan
    0

    Yep, I forgot to set useLegacySchema to "true" initially and then went about my normal installs / config changes, causing myself a mess. Good time to start using the new way anyway ;)

Please Sign in or register to post replies

Write your reply to:

Draft