<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
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 :-)
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
I have managed to essentially do that using the following xslt (please note I am running umbraco v4.7.1.1)
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.
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:
Feel free to ask, if you have any questions about how this works :-)
/Chriztian
Hi,
Thanks for the reply. I will give that a go and get back to you...much appreciated.
Richard
is working on a reply...