Copied to clipboard

Flag this post as spam?

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


  • Floffy 26 posts 33 karma points
    Jun 02, 2012 @ 18:34
    Floffy
    0

    list newest media items under folder

    Hi,

    Using Umbraco 4.7.1

    I'm trying to list 5 newest mediaitems under a folder, having folders in it:

    In the example i should get 6,7 and 8 fra the two folders

    "root"-Folder
    Folder 1 File 1 .... File 7 Folder 2 ... File 6 File 8

    Trying to use the code below indicate that I only gets the first level.
    And how do I limit the number of nodes?

    ...
    <
    xsl:variable name="folder" select="umbraco.library:GetMedia($folderid,1)/*"/>
      <xsl:apply-templates select="$folder[not(self::Folder)]">
        <xsl:sort select="@nodeName" order="descending"/>
      </xsl:apply-templates>
    ...
    <xsl:template match="File">...
    <xsl:template match="Image">...


  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 03, 2012 @ 05:37
    Fuji Kusaka
    0

    Hi floffy,

    I once did something alike using the paramater mediaCurrent in my macro. My Xslt looks like this where i checked all the subfolders for any new item.

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

    <xsl:template match="/">

    <!-- start writing XSLT -->
      
     <xsl:if test="$mediaFolder">
        <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($mediaFolder/*/@id, true())/Folder"/> // looks in all sub folders for new items
        <xsl:for-each select="$mediaItems/File">// loop and display items with MediaType File
          <xsl:sort select="@createDate" order="descending" />
                       <li>            <a href="{umbracoFile}" target='_blank'><xsl:value-of select="@nodeName"/></a>&nbsp; -   
                    <small><xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'dddd dd  MMMM yyy | hh:mm tt')" /></small>
                  </li>
    </xsl:if>
        </xsl:for-each>

     

    Hope it helps

  • Floffy 26 posts 33 karma points
    Jun 03, 2012 @ 21:53
    Floffy
    0

    Thanks to Fuji

    here is the result:

      <xsl:param name="folder" select="/macro/lbkMediaFolderId"/>
        
        <xsl:template match="/">
          <xsl:if test="$folder">
            <ul>
            <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($folder/*/@id, true())/Folder"/
     
            <xsl:for-each select="$mediaItems/File">
              <xsl:sort select="@createDate" order="descending" />
              <xsl:if test="position() &lt;= 5" >
                <li>
                  <a href="{./umbracoFile}" title="&#197;ben {./umbracoExtension} dokument" target='_blank'>
                    <xsl:value-of select="@nodeName"/>
                  </a>
                </li>
              </xsl:if>
            </xsl:for-each>
          </ul>
         </xsl:if>
      </xsl:template>
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 03, 2012 @ 21:58
    Chriztian Steinmeier
    0

    Hi Floffy,

    You're actually off to a great start - so let's just continue from what you have already ('coz you've organised it neatly with templates... pluspoints given :-)

    To get all levels just require a couple of simple changes - keep the $folder variable pointing to the "root" Folder so you're not confusing things (remove the /*), and then make sure to apply templates to descendants of that folder (if you know they're only of the File type - use that instead):

    <xsl:variable name="folder" select="umbraco.library:GetMedia($folderid, 1)"/>
    
    <xsl:apply-templates select="$folder//*[@nodeTypeAlias][not(self::Folder)]">
    <!-- Or maybe only Files: ? -->
    <xsl:apply-templates select="$folder//File" />
    

    - to limit them to only the 5 newest, you'll need to sort and then test the position() - you're using @nodeName to sort - will that work? Can you be sure the naming will always sort correctly? Otherwise you should use the @createDate or @updateDate attributes. If you need to use position() within the item templates, you can keep the apply-templates structure (A), otherwise it probably reads a little better with a for-each (B) in this case... so:

    <!-- (A) -->
    <xsl:apply-templates select="$folder//*[@nodeTypeAlias][not(self::Folder)]">
        <xsl:sort select="@createDate" data-type="text" order="descending" />
    </xsl:apply-templates>
    
    <xsl:template match="File">
        <xsl:if test="position() &lt;= 5">
            <!-- ... -->      
        </xsl:if>
    </xsl:template>
    
    <!-- (B) -->
    <xsl:for-each select="$folder//*[@nodeTypeAlias][not(self::Folder)]">
        <xsl:sort select="@createDate" data-type="text" order="descending" />
        <xsl:if test="position() &lt;= 5">
            <xsl:apply-templates select="." />
        </xsl:if>
    </xsl:for-each>
    
    <xsl:template match="File">
        <!-- ... -->
    </xsl:template>
    

    /Chriztian 

Please Sign in or register to post replies

Write your reply to:

Draft