|
|
 Rank: Devotee
Joined: 8/5/2007 Posts: 59 Location: Bergen, Norway
|
Is there a way to get a random subnode in Umbraco? I have this XSLT: Code:
<a href="{umbraco.library:NiceUrl(@id)}">
<img alt="{@nodeName}">
<xsl:attribute name="src">
/umbraco/ImageGen.aspx?image=
<xsl:value-of select="umbraco.library:GetMedia(./node/data [@alias = 'FullImage'], 'false')/data [@alias = 'umbracoFile']"/>
&height=125&width=125&Pad=true
</xsl:attribute>
</img>
</a>
What I need is to get a random media file inside the gallery as a thumbnail. The above code works, but only gets the first node. www.eyecatch.no
|
|
 Rank: Umbracoholic
Joined: 9/8/2006 Posts: 1,410 Location: KY, USA
|
If, like me, you keep all your site's banner images in a single area of the content tree, you can use the following XSLT to select a random image to display: Code: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp " "> ]> <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="bannerFolder" select="1205"/>
<!-- ============================================================= -->
<xsl:template match="/"> <xsl:variable name="numNodes" select="count(umbraco.library:GetXmlNodeById($bannerFolder)/node)"/> <xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
<img alt="Our Banner Image"> <xsl:attribute name="src"> <xsl:value-of select="umbraco.library:GetXmlNodeById($bannerFolder)/node [position() = $random]/data [@alias='bannerImage']"/> </xsl:attribute> </img> </xsl:template>
<!-- ============================================================= -->
</xsl:stylesheet>
You'll notice that I used the Exslt.ExsltMath:random() function. You can also create an inline function of your own in C# or VB.NET or JavaScript if you need even more flexibility. cheers, doug.
MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
|
|
 Rank: Devotee
Joined: 8/5/2007 Posts: 59 Location: Bergen, Norway
|
How can I make a variable that gets a random node inside the gallery? I tried to do this: Code:
<xsl:variable name="numNodes" select="count(umbraco.library:GetXmlNodeById($currentPage)/node)"/>
<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
<xsl:variable name="randomNode" select="umbraco.library:GetXmlNodeById(.)/node [position() = $random]/data [@alias='FullImage']" />
<img alt="{@nodeName}">
<xsl:attribute name="src">
/umbraco/ImageGen.aspx?image=
<xsl:value-of select="umbraco.library:GetXmlNodeById($randomNode)/data [@alias = 'umbracoFile']"/>
&height=125&width=125&Pad=true&BgColor=Transparent
</xsl:attribute>
</img>
But it does not work. I do not have that heavy experience with XSLT, but what I'm trying to do is to select a random node from the gallery and use the media file it links to as thumbnail image. www.eyecatch.no
|
|
 Rank: Devotee
Joined: 8/5/2007 Posts: 59 Location: Bergen, Norway
|
I tried a different way, but it did not work either. Code:<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $totalRecords) + 1"/> ... <xsl:value-of select="umbraco.library:GetMedia(./node [position() = $random]/data [@alias = 'FullImage'], 'false')/data [@alias = 'umbracoFile']"/>
I have the following site structure: Code:-Home -Image Gallery -Designs -Picture1 -Picture2 -Photos -Picture1 -Picture2 -Picture3
The "Picture" nodes has a document type named "Photo", which has a property named "FullImage" of type Media Picker. The images themselves are located in various folders in the "Media" tab in Umbraco. www.eyecatch.no
|
|
 Rank: Umbracoholic
Joined: 9/8/2006 Posts: 1,410 Location: KY, USA
|
Ahh, simple enough. The key is that you're using a MediaPicker and my example used an Upload data type. The difference is that with the Upload data type you get the path of the image, but with the MediaPicker you simply get its ID and you have to get the path to the file from there. You can see this when you look at the img's src= tag in the output to your browser. You probably saw something like src="1353". That's the ID. Here's how you'd do that based on my first example: Code: <img alt="Our Banner Image"> <xsl:attribute name="src"> <xsl:value-of select="umbraco.library:GetXmlNodeById($bannerFolder)/node [position() = $random]/data [@alias='bannerImage']"/> </xsl:attribute> </img>
...becomes...Code: <xsl:variable name="imageID" select="umbraco.library:GetXmlNodeById($bannerFolder)/node [position() = $random]/data [@alias='bannerImage']"/> <img alt="Our Banner Image"> <xsl:attribute name="src"> <xsl:value-of select="umbraco.library:GetMedia($imageID, 0)/data [@alias='umbracoFile']"/> </xsl:attribute> </img>
cheers, doug.
MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
|
|
 Rank: Devotee
Joined: 8/5/2007 Posts: 59 Location: Bergen, Norway
|
I think that the reason it does not work, is that the xpath is not correct. The xslt for the page: Code: <xsl:template name="listNodes"> <xsl:param name="node"/>
<xsl:for-each select="$currentPage/node [string(./data [@alias='umbracoNaviHide']) != '1']">
<!-- only the nodes for this page --> <xsl:if test="position() >= $startMatch and position() <= $endMatch"> <xsl:call-template name="listNodes"> <xsl:with-param name="node" select="."/> </xsl:call-template> </xsl:if> </xsl:for-each>
listNodes: Code:<xsl:variable name="imageID" select="umbraco.library:GetXmlNodeById($currentPage/node)/node [position() = $random]/data [@alias='FullImage']"/>
<div class="Item"> <!-- get photo thumbnail --> <a href="{umbraco.library:NiceUrl(@id)}"> <img class="fading"> <xsl:attribute name="src"> /umbraco/ImageGen.aspx?image= <xsl:value-of select="umbraco.library:GetMedia($imageID, 0)/data [@alias='umbracoFile']"/> &height=125&width=125&pad=true </xsl:attribute> </img> </a> </div>
www.eyecatch.no
|
|
|
Guest |