Copied to clipboard

Flag this post as spam?

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


  • Greg McMullen 49 posts 195 karma points
    Jan 17, 2013 @ 17:48
    Greg McMullen
    0

    Converting Media Picker XSLT to Razor

    Okay, I don't want a complete answer to this problem. But we are trying to convert our XSLT files to Razor and I am having a difficult time accessing some images from a Media Picker type and displaying them.

    We are trying to display 3 images on a single page each with different alias (i.e. ad1image, ad2image, ad3image). Here is the XSLT that works for this setup. I would love some explanation as to what the variable "class" and "mediaId" are doing and what would be a method to try to convert this file to Razor.

    Thanks in advance.

    <?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="class" select="/macro/class"/>
    
    <xsl:variable name="mediaId" select="/macro/mediaId"/>
    <xsl:template match="/">
      <xsl:if test="$mediaId &gt; 0">
        <xsl:variable name="image" select="umbraco.library:GetMedia($mediaId,'true')"/>
        <img src="{$image/umbracoFile}">
    
          <xsl:if test="$class != ''">
            <xsl:attribute name="class">
              <xsl:value-of select="$class"/>
            </xsl:attribute>
          </xsl:if>
    
          <xsl:if test="/macro/style != ''">
                  <xsl:attribute name="style"><xsl:value-of select="/macro/style"/></xsl:attribute>
          </xsl:if>
        </img>
      </xsl:if>
    </xsl:template>
    
    </xsl:stylesheet>
  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Jan 17, 2013 @ 22:29
    Jason Prothero
    100

    Both class and mediaid are macro parameters passed in from the Template.  The "class" appears to simply be the class name to apply to the <img> tag.  The "mediaid" is likely from a MediaPicker which translates the media selected to an id.  

    This line:

    <xsl:variablename="image"select="umbraco.library:GetMedia($mediaId,'true')"/>

    takes the mediaid and gets the XML data for the media item (image).  Inside that media XML is a node called "umbracoFile" which contains the file path to the image.  

    Also, there is a "style" parameter that is passed in with adds a style attribute to the <img> tag if present.

    Does that help?

  • Greg McMullen 49 posts 195 karma points
    Jan 18, 2013 @ 15:07
    Greg McMullen
    0

    Jason, 

    Thanks for helping me understand the Parameter aspect of macros. This was extremely helpful. With your help and some other forum posts, I was able to get our code working.

    The final code for the project ended up being:

    @{
    var media = new umbraco.cms.businesslogic.media.Media(Convert.ToInt32(@Parameter.mediaId));
    string imageUrl = media.getProperty("umbracoFile").Value.ToString();
    <img src="@imageUrl" /> }

    Images displayed just fine and I now have a better understanding of parameters and (hopefully) more about the Media Picker.

  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Jan 18, 2013 @ 18:35
    Jason Prothero
    0

    Excellent!  Glad I could help, and thanks for posting your final code.  That will help others in your situation in the future!

     

    -Jason

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies