Copied to clipboard

Flag this post as spam?

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


  • Richard 4 posts 25 karma points
    May 27, 2012 @ 14:53
    Richard
    0

    Selecting images from a media folder and counting

    Hello, 

    If you're inside on a sunny day like today looking at this thank you. 

    I am a total beginner when it comes to writing and correctly formatting xslts, so was wondering if someone could help me.

    The task

     

    1. Choose a folder of images uploaded to the media library and associate it with a particular article.
    2. Having chosen a folder display those images on a page in a carousel (jquery).
    So far

    I have managed to essentially do that using the following xslt (please note I am running umbraco v4.7.1.1)
    <?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="imageFolderPicker" select="number($currentPage/imageFolderPicker)" />

    <xsl:template match="/">

    <div id="slider">  
      
      <!-- Displays all images from a folder in the Media Library -->
        
      <xsl:if test="number($imageFolderPicker)">
        
        <xsl:for-each select="umbraco.library:GetMedia($imageFolderPicker, true())/Image">
          
          <xsl:if test="umbracoFile !=''">
            <img src="{umbracoFile}" alt="{@nodeName}" />
          </xsl:if>
          
        </xsl:for-each>
        
      </xsl:if>

    </div>

    </
    xsl:template>

    </
    xsl:stylesheet>

    This is ok, it spits out the images as intended, but it doesn't take into account a scenario where there is only one image in a folder.

    I know I need to do a choose statement so that when on count of the images if it is 1 do x, when it is 0 do y, otherwise do z.

    I seem to be stumbling on the correct syntax, and most examples I can find refer to older versions of umbraco, any help would be appreciated

    Many Thanks.

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 27, 2012 @ 21:15
    Chriztian Steinmeier
    0

    Hi Richard,

    You *can* use a choose instruction to do that, but often times it depends on what you're trying to do... - here's a very "XSLT-ish" approach to something like that:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
    
        <xsl:template match="/">
            <!-- Only do something if the imageFolderPicker on $currentPage has a value -->
            <xsl:if test="normalize-space($currentPage/imageFolderPicker)">
                <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($currentPage/imageFolderPicker, true())" />   
                <!-- If the folder exists -->
                <xsl:if test="not($mediaNode[error])">
                    <!-- Run the template for a Folder -->
                    <xsl:apply-templates select="$mediaNode" />
                </xsl:if>
            </xsl:if>
        </xsl:template>
    
        <!-- Standard template for a Folder -->
        <xsl:template match="Folder">
            <div id="slider">
                <!-- Change the id to "still" if only one Image -->
                <xsl:if test="count(Image) = 1"><xsl:attribute name="id">still</xsl:attribute></xsl:if>
    
                <!-- Run templates for all Image elements in folder -->
                <xsl:apply-templates select="Image" />
            </div>
        </xsl:template>
    
        <!-- Template for all Image elements -->
        <xsl:template match="Image">
            <img src="{umbracoFile}" alt="{@nodeName}" />
        </xsl:template>
    
    </xsl:stylesheet>

     

    Feel free to ask, if you have any questions about how this works :-)

    /Chriztian

  • Richard 4 posts 25 karma points
    May 28, 2012 @ 10:40
    Richard
    0

    Hi, 

    Thanks for the reply. I will give that a go and get back to you...much appreciated.

    Richard

Please Sign in or register to post replies

Write your reply to:

Draft