Copied to clipboard

Flag this post as spam?

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


  • Stefan 117 posts 215 karma points
    Jan 09, 2010 @ 01:42
    Stefan
    0

    Check if media exists and do

    Hi.

    I have been trying to check if a media string is empty or not. I want to set different variables to ImageGen if the string contains no media (to set a "No picture" text instead of "Hello world"). This does not apply for a string containing any other media, since ImageGen would write the text on top of that image?

    Heres some of my code - it should be fairly easy to do I think :)

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

    <xsl:choose>

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

        Do this (some of my code):

        <img>
            <xsl:attribute name="title">
                <xsl:value-of select="@nodeName" />
            </xsl:attribute>
            <xsl:attribute name="alt">
                <xsl:value-of select="@nodeName" />
            </xsl:attribute>
            <xsl:attribute name="src">
                <xsl:text>/umbraco/ImageGen.ashx?image=</xsl:text>
                <xsl:value-of select="(data [@alias = 'productPic'])" />
                <xsl:text>&amp;fontcolor=gray</xsl:text>
                <xsl:text>&amp;text=NO-PICTURE</xsl:text>
                <xsl:text>&amp;VAlign=top</xsl:text>
                <xsl:text>&amp;width=250</xsl:text>
                <xsl:text>&amp;height=200</xsl:text>
                <xsl:text>&amp;constrain=true</xsl:text>
            </xsl:attribute>
        </img>

        </xsl:when>
       
        <xsl:otherwise>
       
            Otherwise do this (some of my code):
           
            <img>
            <xsl:attribute name="title">
                <xsl:value-of select="@nodeName" />
            </xsl:attribute>
            <xsl:attribute name="alt">
                <xsl:value-of select="@nodeName" />
            </xsl:attribute>
            <xsl:attribute name="src">
                <xsl:text>/umbraco/ImageGen.ashx?image=</xsl:text>
                <xsl:value-of select="(data [@alias = 'productPic'])" />
                <xsl:text>&amp;Align=center</xsl:text>
                <xsl:text>&amp;VAlign=top</xsl:text>
                <xsl:text>&amp;width=250</xsl:text>
                <xsl:text>&amp;height=200</xsl:text>
                <xsl:text>&amp;constrain=true</xsl:text>
            </xsl:attribute>
        </img>

        </xsl:otherwise>
       
    </xsl:choose>

    </xsl:for-each>

     

    Any suggestions? I can get it to work one way or another - but not both ways.

    Thanks.

  • Tommy Poulsen 514 posts 708 karma points
    Jan 09, 2010 @ 10:47
    Tommy Poulsen
    0

    Hi Stefan, could you elaborate a bit on the problem - what works and what doesn't? What happens when it doesn't work?

     

    >Tommy

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Jan 09, 2010 @ 11:55
    Douglas Robar
    0

    You can certainly simplify your xslt a lot (example shown below), but there's an even easier way to automatically handle missing images with ImageGen...

    Create an image to display when the requested image is missing. To do this, create an image that suits your needs, say one that is 250x200 in size with the text NO-PICTURE on it. Save it as a file to your website. Let's assume this is saved in the file, /media/no-picture.jpg. You could then use the following in your xslt:

         <img title="{@nodeName} alt="{@nodeName}">
            <xsl:attribute name="src">
               
    <xsl:text>/umbraco/ImageGen.ashx?image=</xsl:text>
               
    <xsl:value-of select="(data [@alias = 'productPic'])" />
                <xsl:text>&amp;altImage=/media/no-picture.jpg</xsl:text>
                <xsl:text>&amp;Align=center</xsl:text>
               
    <xsl:text>&amp;VAlign=top</xsl:text>
               
    <xsl:text>&amp;width=250</xsl:text>
               
    <xsl:text>&amp;height=200</xsl:text>
               
    <xsl:text>&amp;constrain=true</xsl:text>
           
    </xsl:attribute>
       
    </img>

    That uses the altImage= parameter in ImageGen, which will show the alternate image only if the image= parameter has no file associated with it.

     

    But, if you wanted to do something like your original question in xslt rather than with the altImage= parameter, I'd simplify it to this:

         <img title="{@nodeName} alt="{@nodeName}">
            <xsl:attribute name="src">
               
    <xsl:text>/umbraco/ImageGen.ashx</xsl:text>
                <xsl:text>?Align=center</xsl:text>
               
    <xsl:text>&amp;VAlign=top</xsl:text>
               
    <xsl:text>&amp;width=250</xsl:text>
               
    <xsl:text>&amp;height=200</xsl:text>
               
    <xsl:text>&amp;constrain=true</xsl:text>
    <xsl:choose>
    <xsl:when test="string(data[@alias='productPic']) != ''">
    <xsl:text>&amp;image=</xsl:text>
                <xsl:value-of select="(data [@alias = 'productPic'])" />
    </xsl:when>
    <xsl:otherwise>
                <xsl:text>&amp;fontcolor=gray</xsl:text>
               
    <xsl:text>&amp;text=NO-PICTURE</xsl:text>
    </xsl:otherwise>
    </xsl:choose>
           
    </xsl:attribute>
       
    </img>

    cheers,
    doug.

  • Stefan 117 posts 215 karma points
    Jan 09, 2010 @ 14:50
    Stefan
    0

    Hi Douglas!

    Thank you for the exact and well-explained answer(s). Didn't know about the altImage option in ImageGen.

    Will certainly test the two possibilities :)

    Thanks again!

Please Sign in or register to post replies

Write your reply to:

Draft