Copied to clipboard

Flag this post as spam?

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


  • J 447 posts 864 karma points
    Jul 15, 2013 @ 17:59
    J
    0

    URL against image

    I have some XSLT code that displays images - this is fine. What i would like to do is add a URL to the image so when the image is clicked it navigates to the address. i have the below code but not sure how i could add the href tag with the address?

            <xsl:if test="$currentPage/Image != ''">  

              <xsl:element name="img">

                <xsl:attribute name="src">

                  <xsl:value-of select="umbraco.library:GetMedia($currentPage/Image, 0)/*"/>

                </xsl:attribute>

     <xsl:attribute name="alt">

                  <xsl:value-of select="$currentPage/Image"/>

                </xsl:attribute>

              </xsl:element>

            </xsl:if>

    Could anyone advise?

    Thanks

  • Sebastian Dammark 583 posts 1407 karma points
    Jul 16, 2013 @ 02:53
    Sebastian Dammark
    1

    Hi J

    As I worked through the scenario I stumbled upon alot of stuff that could happen.  What if there is no link or no image.  The link, is it internal or external.
    So I made 2 examples 

    Example 1 (Simple):

    <xsl:variable name="mediaData" select="umbraco.library:GetMedia($currentPage/Image, 0)" />
    
    <xsl:if test="$mediaData/Image[normalize-space()]">
        <a href="{$currentPage/imageLink}">
            <img src="{$mediaData/Image/umbracoFile}" alt="{$mediaData/Image/umbracoFile}" />
        </a>
    </xsl:if>

     

    Example 2 (advanced): 

    // First we get the media data
    <xsl:variable name="mediaData" select="umbraco.library:GetMedia($currentPage/Image, 0)" />
    
    // Then we apply a template to the variable called mediaData, as long as it doesn't contain an error node
    // mediaData will contain an error node when GetMedia is called with a non-valid media id
    <xsl:apply-templates select="$mediaData[not(error)]" mode="media" />
    
    // This template matches all Image nodes descending the mediaData variable, that doesn't contain an error node
    <xsl:template match="Image" mode="media">
        <xsl:choose>
            // If the imageLink property isn't empty, we apply a template to it.
            <xsl:when test="$currentPage/imageLink[normalize-space()]">
                <xsl:apply-templates select="$currentPage/imageLink" />
            </xsl:when>
    
            // Otherwise we apply a template to the image file.
            <xsl:otherwise>
                <xsl:apply-templates select="umbracoFile[normalize-space()]" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    // This template matches the imageLink property
    <xsl:template match="imageLink">
        <xsl:variable name="tempUrl">
            <xsl:choose>
                // If the imageLink property is an external URL it will contain at least 1 '.'
                <xsl:when test="contains(., '.')">
                    <xsl:value-of select="." />
                </xsl:when>
    
                // Otherwise we assume it's an internal link and call the NiceUrl extension with the link id.
                <xsl:otherwise>
                    <xsl:value-of select="umbraco.library:NiceUrl(.)" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
    
        <a href="{$temUrl}">
            <xsl:apply-templates select="umbracoFile[normalize-space()]" />
        </a>
    </xsl:template>
    
    // This template matches the actual link to the image
    <xsl:template match="umbracoFile">
        <img src="{.}" alt="{.}" />
    </xsl:template>
    
    // This template matches all nodes, except Image nodes, descending the mediaData variable.  Like video, mp3, pdf or nodes containing an error node.
    <xsl:template match="*|*[error]" mode="media" />

    I haven't tested any of the examples so I hope it works ... :)

  • J 447 posts 864 karma points
    Jul 16, 2013 @ 10:23
    J
    0

    Thanks for that. Im new with XSLT so the simple example has got me confused :(

    Should i replace my code with your simple example? If yes i tried that and i got an error (System.OverflowException: Value was either too large or too small for an Int32. ).

    Or how should my existing code be changed to cater for the change i require? If possible could you base it on my code so i understand it for next time too please?

    Thanks

  • Sebastian Dammark 583 posts 1407 karma points
    Jul 18, 2013 @ 00:58
    Sebastian Dammark
    0

    Sorry for my late reply.

    When you get the "Value was either too large or too small for an Int32" error, it's usually because that $currentPage/Image is empty.

    So yiou might wanna check that first.

Please Sign in or register to post replies

Write your reply to:

Draft