Copied to clipboard

Flag this post as spam?

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


  • Alex 9 posts 30 karma points
    Aug 10, 2011 @ 16:34
    Alex
    0

    Random image from media folder.

    Using Umbraco 4.7

    Can't seem to get this to work.
    I changed out $mediaFolder for the direct id to test.

    I just get a blank <img/> tag.

     

    <?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.ExsltMath="urn:Exslt.ExsltMath"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltMath">

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

    <xsl:param name="currentPage"/>
          <xsl:variable name="mediaFolder" select="/macro/folder" />
          
    <xsl:template match="/">
        <xsl:variable name="imgFolder" select="umbraco.library:GetMedia(1150, 'false')"/>
        <xsl:variable name="imgRandom" select="floor(Exslt.ExsltMath:random() * count($imgFolder/node) + 1)"/>
        <xsl:variable name="img" select="$imgFolder/node[position()=$imgRandom]/data[@alias='umbracoFile']" />
        <img src="{$img}" alt="" />
    </xsl:template>
          
    </xsl:stylesheet>

  • Richard 146 posts 168 karma points
    Aug 10, 2011 @ 16:46
    Richard
    0

    I think that $imgFolder does not contain "./node" elements, but File sub nodes. So count($imgFolder/node) returns zero. I think everywhere you have node you need File.

    Richard

  • Alex 9 posts 30 karma points
    Aug 10, 2011 @ 17:14
    Alex
    0

    Almost there,

    <xsl:variable name="mediaFolder" select="/macro/folder" />

    <xsl:template match="/">
      <xsl:copy-of select="$mediaFolder" />
        <xsl:if test="$mediaFolder != ''">
        <xsl:variable name="imgFolder" select="umbraco.library:GetMedia($mediaFolder, 'false')"/>
        <xsl:variable name="imgRandom" select="floor(Exslt.ExsltMath:random() * count($imgFolder/Image) + 1)"/>
        <xsl:variable name="img" select="$imgFolder/Image[position()=$imgRandom]/umbracoFile" />
        <img src="{$img}" alt="" />
      </xsl:if>
    </xsl:template>  

    Just cant get $mediaFolder to work now.

    It's a "mediaCurrent" type in the macro.

  • Richard 146 posts 168 karma points
    Aug 10, 2011 @ 17:20
    Richard
    0

    I would try the following:

    1. Check the output from your copy-of statement, is this blank? You need to look at the source code to see this.
    2. If this is blank, check that you are passing in a picked media folder, or valid mediaID in the macro call
    3. Check that the case of the parameter folder matches.
    4. If you are still not getting a value passed from the template (where the macro is called) to the XSLT, then change the argument name from folder to say mediaFolder, in case it is a reserved word.
  • Alex 9 posts 30 karma points
    Aug 10, 2011 @ 17:27
    Alex
    0

    A copy-of produces this,

     

    <folder>
    <folder id="1150" path="-1,1082,1150" nodetypealias="Folder" writername="admin" urlname="header" nodename="Header" updatedate="2011-08-10T14:57:05" createdate="2011-08-10T14:57:05" sortorder="7" template="0" nodetype="1031" writerid="0" level="2" parentid="1082" version="a3e11899-01b6-4ce8-b0b0-ea9fba23edc5">
    </folder>
    </folder> 
  • Richard 146 posts 168 karma points
    Aug 10, 2011 @ 18:33
    Richard
    0

    Alex,

    You need to get the id attribute of the folder tag, which will be $mediaFolder/folder/@id, and pass this into the GetMedia method.

    Richard

     

  • Alex 9 posts 30 karma points
    Aug 11, 2011 @ 10:54
    Alex
    1

    Turns out it was a case issue, the "folder" in the variable needed a capital F.

     

    <?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.ExsltMath="urn:Exslt.ExsltMath"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltMath">

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

    <xsl:param name="currentPage"/>
    <xsl:variable name="mediaFolder" select="/macro/folder/Folder/@id" />
    <xsl:variable name="css" select="/macro/imageCSSClass" />

    <xsl:template match="/">
      <xsl:if test="$mediaFolder > 0">
        <xsl:variable name="imgFolder" select="umbraco.library:GetMedia($mediaFolder, 'false')"/>
        <xsl:variable name="imgRandom" select="floor(Exslt.ExsltMath:random() * count($imgFolder/Image) + 1)"/>
        <xsl:variable name="img" select="$imgFolder/Image[position()=$imgRandom]/umbracoFile" />
        <img src="{$img}" alt="" class="{$css}" />
      </xsl:if>
    </xsl:template>
          
    </xsl:stylesheet>  

  • Richard 146 posts 168 karma points
    Aug 11, 2011 @ 10:59
    Richard
    0

    Good to see how you resolved your problem.

Please Sign in or register to post replies

Write your reply to:

Draft