Copied to clipboard

Flag this post as spam?

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


  • Daniel Dawson 27 posts 47 karma points
    Jul 07, 2011 @ 22:52
    Daniel Dawson
    0

    How to show img tag only when an image file has been uploaded?

    Hi guys,

    Here's the situation. I have a page called portfolio post which displays a specific post within my portfolio. In my doc type I have 2 upload file fields - one called umbracoFile and umbracoFile1 (I know I know.. I need to rename these), and a richtext editor field named bodyText.

    All portfolio posts will have an image uploaded to umbracoFile but an image upload for umbracoFile1 is not mandatory. When I don't upload a file to umbracoFile1 and run my page it shows the broken image symbol (which is to be expected as nothing has been entered inbetween the img tag). Is there another way around this to avoid the second <img> tag showing if nothing has been uploaded to the umbracoFile1 field?

    Here's my code for the right content section:

    <div id="right-content">
        <div class="portfolio-container">
        <img src="<umbraco:Item field="umbracoFile" runat="server"></umbraco:Item>" width="460px" height="40%" />
        </div>
        <div class="portfolio-container">
        <img src="<umbraco:Item field="umbracoFile1" runat="server"></umbraco:Item>" width="460px" height="40%" />
        </div>
      </div>

    Thanks!

    Dan

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 07, 2011 @ 23:15
    Dennis Aaen
    0

    Hi Dan,

    It sounds like this is a task for XSLT. I do not know how familiar you are with XSLT.

    But it is possible to get an image out via xslt. In XSLT, it is also possible to check whether you selected a picture in media picker, if not we dont print the HTML markup for the picture

    I thnik this code so do the job for you

    <xsl:if test="$currentPage/umbracoFile!=''">   
        <div class="portfolio-container">
            <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/umbracoFil, 0)" />
               <img src="{$media/umbracoFile}"/>
        </div>
     </xsl:if>
     
     <xsl:if test="$currentPage/umbracoFile1!=''">  
        <div class="portfolio-container">
            <xsl:variable name="media1" select="umbraco.library:GetMedia($currentPage/umbracoFil1, 0)" />
               <img src="{$media1/umbracoFile}"/>
        </div>
     </xsl:if>

    The <xsl;if test> test if the media picker has any content if, the markup will get printed out, if not no markup will be printed.

    Hope this can helps you.

    /Dennis

     

  • Daniel Dawson 27 posts 47 karma points
    Jul 07, 2011 @ 23:30
    Daniel Dawson
    0

    Hi Dennis,

    Thanks a lot for the quick reply! I am quite new to XSLT so I'm still learning lots of things about it.

    I've tried your code in the XSLT File:

    <?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:CWS.Twitter="urn:CWS.Twitter" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets CWS.Twitter PS.XSLTsearch ">


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:if test="$currentPage/umbracoFile!=''">   
        <div class="portfolio-container">
            <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/umbracoFil, 0)" />
               <img src="{$media/umbracoFile}"/>
        </div>
     </xsl:if>
     
     <xsl:if test="$currentPage/umbracoFile1!=''">   
        <div class="portfolio-container">
            <xsl:variable name="media1" select="umbraco.library:GetMedia($currentPage/umbracoFil1, 0)" />
               <img src="{$media1/umbracoFile}"/>
        </div>
     </xsl:if>
      
    </xsl:template>

    </xsl:stylesheet>

    However, I get the following error on my page in the right container  - "Error parsing XSLT file: \xslt\PortfolioPost.xslt".

    Am I doing something wrong?

    Thanks again.

    Dan

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 07, 2011 @ 23:37
    Dennis Aaen
    1

    Hi Dan,

    Made a small mistake in previous post

    try to change:

    <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/umbracoFil1, 0)" />

    and <xsl:variable name="media1" select="umbraco.library:GetMedia($currentPage/umbracoFil, 0)" />

    To:

    xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/umbracoFile, 0)" />

    and <xsl:variable name="media1" select="umbraco.library:GetMedia($currentPage/umbracoFile1, 0)" />

    Hope it goes better this time.

    /Dennis

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 07, 2011 @ 23:42
    Michael Latouche
    0

    Hi Daniel,

    I don't know if it's the reason of the error, but I think you mispelled the "path" in your calls to GetMedia: you use "$currentPage/umbracoFil" and "$currentPage/umbracoFil1" insead of "$currentPage/umbracoFile" and "$currentPage/umbracoFile1" (notice the final "e").

    So I guess you would heav mor luck with this:

    <?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:CWS.Twitter="urn:CWS.Twitter" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets CWS.Twitter PS.XSLTsearch ">


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:if test="$currentPage/umbracoFile!=''">   
        <div class="portfolio-container">
            <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/umbracoFile, 0)" />
               <img src="{$media/umbracoFile}"/>
        </div>
     </xsl:if>
     
     <xsl:if test="$currentPage/umbracoFile1!=''">   
        <div class="portfolio-container">
            <xsl:variable name="media1" select="umbraco.library:GetMedia($currentPage/umbracoFile1, 0)" />
               <img src="{$media1/umbracoFile}"/>
        </div>
     </xsl:if>
      
    </xsl:template>

    </xsl:stylesheet>

     

     Hope this helps.

    Cheers,

    Michael 

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 07, 2011 @ 23:42
    Michael Latouche
    1

    OK so I was too slow to type ;-)

  • Daniel Dawson 27 posts 47 karma points
    Jul 07, 2011 @ 23:47
    Daniel Dawson
    0

    Hi Dennis, I still get the same error on my page..I've checked to make sure my data type name and alias's are correct as well just in case. I can't see anything wrong with the XSLT code, can you?

    <?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:CWS.Twitter="urn:CWS.Twitter" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets CWS.Twitter PS.XSLTsearch ">


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:if test="$currentPage/umbracoFile!=''">   
        <div class="portfolio-container">
            <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/umbracoFile, 0)" />
               <img src="{$media/umbracoFile}" width="460px" height="40%"/>
        </div>
     </xsl:if>
     
     <xsl:if test="$currentPage/umbracoFile1!=''">   
        <div class="portfolio-container">
            <xsl:variable name="media1" select="umbraco.library:GetMedia($currentPage/umbracoFile1, 0)" />
               <img src="{$media1/umbracoFile}" width="460px" height="40%"/>
        </div>
     </xsl:if>
      
    </xsl:template>

    </xsl:stylesheet>

    Thanks a lot for your help.

    Dan

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 08, 2011 @ 00:02
    Dennis Aaen
    0

    I think sounds a little weird.

    Which version of Umbraco do you use. I just tried to do the setup you describe locally and copy  your file and I get the images out. Both images, if both media pickers has content otherwise only have a picture. I´m using the 4.7 of Umbraco.

    I used your code from your last post. So that's why I think it's a little weird that it does not work with you.

    /Dennis

  • Daniel Dawson 27 posts 47 karma points
    Jul 08, 2011 @ 00:09
    Daniel Dawson
    0

    Ahhh I think I know what the problem is. The umbracoFile and umbracoFile1 currently uses the 'Upload' data type, not 'Media Picker' data type. Is this right?

    How can I use the XSLT code for a a file upload data type for umracoFile and umbracoFile?

    Thanks Dennis

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 08, 2011 @ 00:09
    Michael Latouche
    0

    Wild guess, but could it be that your macro was cached and that you did not actually run the new version of it the secont time?

    Otherwize you can "debug" the macro by removing most logic, check that you don't get any error, then add the lines "one bye one" and test every time. This way you will at least know where it is going wrong and we can focus on that :-)

    Cheers,

    Michael.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 08, 2011 @ 00:41
    Dennis Aaen
    0

    Hi again Dan,

    Is it completely impossible to use the media picker data type instead of uploading the data type in your setup?. Because that is exactly what goes wrong. Have just tested it with the upload the data type and then comes to pictures out.

    /Dennis

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Jul 08, 2011 @ 01:10
    Chriztian Steinmeier
    0

    Hi Daniel,

    Here's a very simple approach:

    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
        <xsl:apply-templates select="$currentPage/umbracoFile[normalize-space()]" />
        <xsl:apply-templates select="$currentPage/umbracoFile1[normalize-space()]" />
    </xsl:template>
    
    <xsl:template match="umbracoFile | umbracoFile1">
        <div class="portfolio-container">
            <img src="{.}" width="460" height="40%" />
        </div>
    </xsl:template>
    
    (The normalilze-space() function makes sure that the field isn't empty)
    BTW: Do you really mean to size the image like that (fixed width and 40% height)? Seems to me that that could create some weird issues with distorted images... 

    /Chriztian

  • Daniel Dawson 27 posts 47 karma points
    Jul 08, 2011 @ 15:22
    Daniel Dawson
    0

    Your the main Chriztian! Thank you! It works using it your way! No I'm not planning on keeping it like that using fixed width and 40% height. Would you advise using something like the image cropper to display the images? Or is there a better method?

    Thanks a lot also Dennis, if I decide to go down the media picker route I would deffinately use your method!

  • Daniel Dawson 27 posts 47 karma points
    Jul 08, 2011 @ 15:36
    Daniel Dawson
    0

    Or just leave the height parameter blank and keep the width fixed?

Please Sign in or register to post replies

Write your reply to:

Draft