Copied to clipboard

Flag this post as spam?

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


  • robin 12 posts 72 karma points
    May 18, 2017 @ 16:31
    robin
    0

    xslt for showing image

    Okay.. I got this far:

    <umbraco:Macro Alias="ShowImage" imageField="box1Image" runat="server"></umbraco:Macro>
    

    And XLST:

    <?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" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
    
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
          <xsl:variable name="imageField" select="/macro/imageField"/>
    
    
    <xsl:template match="/">
        -
        <xsl:value-of select="$imageField"/>
        <xsl:value-of select="$currentPage/box1Image"/>
        -    
    
        <xsl:variable name="mediaId" select="number($imageField)" />
        <xsl:value-of select="$mediaId"/>
    
        <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="[image]" height="{umbracoHeight}" width="{umbracoWidth}" />
            </xsl:if>
        </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
    

    My problem is that:

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

    Outputs: box1Image and not 21920 So how do I explain to XSLT that this is indeed a MediaPicker and that I want the MediaID and not the String "box1Image", so that I can finish up the job?

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

    Correctly displays 21920.. however I don't want my field hardcoded into my macro, which would remove the whole point of creating a Macro.

    I've looked all over the forums, but for some reason my scenario where I reference the field as a paramter in the Macro doesn't seem to be a use case anyone else uses, so maybe I'm fundamentally misunderstanding something?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 18, 2017 @ 17:13
    Dennis Aaen
    0

    Hi Robin,

    First of all what exact version of Umbraco are you using?

    Best, -Dennis

  • robin 12 posts 72 karma points
    May 18, 2017 @ 17:15
    robin
    0

    umbraco v 4.9.1 (Assembly version: 1.0.4884.22745)

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 18, 2017 @ 17:17
    Dennis Aaen
    0

    Hi Robin,

    Okay thanks for the info. Can you then try to use this snippet of code.

    <xsl:if test="number($currentPage/mainImage) > 0">
        <xsl:variable name="selectedMedia" select="umbraco.library:GetMedia($currentPage/mainImage, 0)" />
        <img src="{$selectedMedia/umbracoFile}" width="{$selectedMedia/umbracoWidth}" height="{$selectedMedia/umbracoHeight}" alt="{$selectedMedia/@nodeName}" />
    </xsl:if>
    

    Remember to change mainimage to the right alias in your case.

    The issue is that you have mix up the old XML Schema which was changed in version 4.5 of Umbraco.

    So this line of code

      <xsl:variable name="mediaId" select="number($currentPage/data[@alias=$imageField])" />
    

    Should be:

      <xsl:variable name="mediaId" select="number($currentPage/imageField)" />
    

    Hope this helps,

    /Dennis

  • robin 12 posts 72 karma points
    May 18, 2017 @ 17:49
    robin
    0

    Thanks for that, though that's correct, that is not the issue I'm having, my issue is that I can get it to work if I directly reference $currentPage/box1Image, but I don't want to do that, I want to indirectly reference it through the parameters of the Macro, so that I can use the macro it load multiple images from different MediaPickers in the same Document/Template.

  • robin 12 posts 72 karma points
    May 18, 2017 @ 17:26
    robin
    0

    Hi Dennis,

    Yeah that works fine.. but in your case you're referencing the image directly with $currentPage/box1Image, where as I want to do it as a parameter on the Macro, so I don't have to create 3 macros for the same job.. having

    <umbraco:Macro Alias="ShowImage" imageField="box1Image" runat="server"></umbraco:Macro>
    <umbraco:Macro Alias="ShowImage" imageField="box2Image" runat="server"></umbraco:Macro>
    <umbraco:Macro Alias="ShowImage" imageField="box3Image" runat="server"></umbraco:Macro>
    

    As appose to:

    <umbraco:Macro Alias="ShowImageBox1Image" runat="server"></umbraco:Macro>
    <umbraco:Macro Alias="ShowImageBox2Image" runat="server"></umbraco:Macro>
    <umbraco:Macro Alias="ShowImageBox3Image" runat="server"></umbraco:Macro>
    

    Hope that makes sense.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 18, 2017 @ 17:53
    Dennis Aaen
    0

    Hi Robin,

    Okay make sense.

    Could you please try this code snippet

    <?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" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:variable name="mediaId" select="/macro/imageField"/>
    <xsl:template match="/">
        <xsl:if test="$mediaId &gt; 0">
        <xsl:variable name="image" select="umbraco.library:GetMedia($mediaId,0)"/>
     <img src="{$image/umbracoFile}" alt="{$image/nodeName}" />
               </xsl:if>
           </img>
      </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
    

    Hope this helps,

    /Dennis

  • robin 12 posts 72 karma points
    May 18, 2017 @ 18:13
    robin
    0

    Hi again,

    Yeah so this is what I've tried myself, or something similar. However the problem is that:

    <xsl:value-of select="$currentPage/box1Image"/> /* returns: 21290 */    
    <xsl:value-of select="$mediaId"/> /* returns: box1Image */
    

    Using this code you provided:

    <?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" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:variable name="mediaId" select="/macro/imageField"/>
    <xsl:template match="/">
    
        <xsl:value-of select="$currentPage/box1Image"/> /* returns: 21290 */    
        <xsl:value-of select="$mediaId"/> /* returns: box1Image */
    
        <xsl:if test="$mediaId &gt; 0">
        <xsl:variable name="image" select="umbraco.library:GetMedia($mediaId,0)"/>
     <img src="{$image/umbracoFile}" alt="{$image/nodeName}" />
      </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
    

    The problem might be the way I'm referencing the field and sending the field "alias" throught?

    My Macro looks like this:

    <umbraco:Macro Alias="ShowImage" imageField="box1Image" runat="server"></umbraco:Macro>
    

    Should I be doing that differently? Somehow extracting the mediaId and sending it to the Macro? I think the problem is that I'm just passing the string "box1Image" along and not the actual MediaPicker box1Image?

    When I reference it in the template:

    <umbraco:Item field='box1Image' runat='server'></umbraco:Item>
    

    It works just fine, but how do I extract the ID?

    Without knowing the Umbraco syntaks I'd think i'd need to extract it somethow.. like:

    <umbraco:Macro Alias="ShowImage" imageField="getMediaIDFromField(box1Image)" runat="server"></umbraco:Macro>
    
  • robin 12 posts 72 karma points
    May 19, 2017 @ 16:52
    robin
    0

    Is there really no one that knows what I'm doing wrong?

Please Sign in or register to post replies

Write your reply to:

Draft