Copied to clipboard

Flag this post as spam?

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


  • Simon Timms 16 posts 36 karma points
    Sep 18, 2009 @ 22:05
    Simon Timms
    0

    Retrieving a list of media

    Hi,

    I'm just starting to play with the XSLT system in umbraco where I was hoping to produce a macro which listed all the media under a specific media directory.  I have come across umbraco.library:GetMedia but, frankly, I have no idea what to pass to it in order to get a list of items.  The API docs at http://umbraco.org/apiDocs/html/M_umbraco_library_GetMedia.htm seem to suggest that what I probably want is to look up a node (how?) and then pass it in with

        umbraco.library:GetMedia(<some node id>, true)

    How would I go about getting that initial node id?

    Subsiquently would something like this work?

    <xsl:for-each select="umbraco.library:GetMedia(<SOMEMAGIC>, 'true')">
        <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
            </a>
        </li>
    </xsl:for-each>

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 18, 2009 @ 23:04
    Thomas Höhler
    0

    Either a hard coded id (the id of the media folder) or a generic property via a media picker.

    Add a media picker to the document type you want where the user can select the media folder to iterate through.

    hth, Thomas

  • Simon Timms 16 posts 36 karma points
    Sep 19, 2009 @ 00:05
    Simon Timms
    0

    That mostly worked.  I retrieved the nodes based on a media picker output, however what I get out seems to be a bit nonsensical.  I did this:

    <xsl:for-each select="umbraco.library:GetMedia($currentPage/data [@alias='mediaDir'], 'true')/node">
            name is:     <xsl:value-of select="."/>
        <li>
         <xsl:for-each select="@*">
             Attribute name=<xsl:value-of select="name()"/><br/>
             Attribute value=<xsl:value-of select="."/><br/>
          </xsl:for-each>
        </li>
    </xsl:for-each>

     

    The inner loop is just querying the node attributes for me to see if they are useful (they aren't).  The <value-of select="."/> returns something like

       /media/303/hydrangeas.jpg1024768595284jpg

    where the actual file name is

      /media/303/hydrangeas.jpg

    What is this extra stuff appended to the end and is it possible to just get the actual path?  I can paste full output but it is a bit verbose. 

  • Roel Snetselaar 151 posts 305 karma points
    Sep 19, 2009 @ 09:14
    Roel Snetselaar
    0

    Hi Simon, Welcom to the Umbraco forum!

    What you need within your for loop is this.

    Media name:

    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='mediaDir'], 'true')/@nodeName"/>

    Media location:

    <xsl:value-of select="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']"/>

     

  • Roel Snetselaar 151 posts 305 karma points
    Sep 19, 2009 @ 09:15
    Roel Snetselaar
    0

    Welcome that is....it's an early morning.

  • Simon Timms 16 posts 36 karma points
    Sep 20, 2009 @ 00:19
    Simon Timms
    0

    That was tantalizingly close to what I wanted.  The only issue is that the media name you gave actually outputs the name of the directory over which I'm running my script.  I managed to get it in the end withjust

    <xsl:when test="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']">

    So the completed script looks like

    <xsl:for-each select="umbraco.library:GetMedia($currentPage/data [@alias='mediaDir'], 'true')/node">
     <li>
       <xsl:choose>
         <xsl:when test="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']">
        <a><xsl:attribute name="href">
         <xsl:value-of select="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']"/>
           </xsl:attribute>
            <xsl:value-of select="@nodeName"/>
        </a>
         </xsl:when>
         <xsl:otherwise>
          <!--Do something with the directory-->
         </xsl:otherwise>
        </xsl:choose>
      </li>
    </xsl:for-each>

    This script can also handle nested directories.  I'm ignoring them at the moment but others might wish to recurse into them such that we could get an entire tree structure.  This feels a lot like something to develop into a package. 

  • Simon Timms 16 posts 36 karma points
    Sep 20, 2009 @ 01:12
    Simon Timms
    0

    He he he, this one checks if the media is an image and if so displays the thumbnail.  This umbraco thing is awsome!

     

    <xsl:if test="$currentPage/data [@alias='mediaDir']">
    <xsl:for-each select="umbraco.library:GetMedia($currentPage/data [@alias='mediaDir'], 'true')/node">
       
     <xsl:choose>
     
        <xsl:when test="@nodeTypeAlias = 'Image'">
     <li>
        <a><xsl:attribute name="href">
         <xsl:value-of select="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']"/>
           </xsl:attribute>
            <img>
                <xsl:attribute name="src">
                    <xsl:value-of select="Exslt.ExsltStrings:replace(umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile'], '.', '_thumb.')"/>
                </xsl:attribute>
            </img>
            <xsl:value-of select="@nodeName"/>
            <xsl:value-of select="@nodeTypeAlias"/>

        </a>

      </li>
        </xsl:when>

        <xsl:otherwise>

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

    </xsl:for-each>
    </xsl:if>

Please Sign in or register to post replies

Write your reply to:

Draft