I have a very simple gallery of images on my site, using the new schema (4.6.1). Because this is such a basic site and there will only ever be a maximum of 5 images in the gallery (there may be fewer) and I want the UI to be as utterly simple as possible, I've decided to add 5 'media picker' data types to the gallery document type, called mainImage1, mainImage2, mainImage3, mainImage4 and mainImage5 rather than adding a container to create 'nodes' of images in a folder as I'd normally do.
Whilst this is the simplest approach from a site admin point of view, I'm struggling with getting the most efficient XSLT to iterate through each of these image data types without repeating the code for each media item.
This code shows the first image, but rather than repeat this whole bit of code 5 times, I'd prefer to use some kind of loop/template mechanism just to iterate over this code 5 times. Make sense?
Actually, what I'm trying to do is give the first image a particular class, but the other images no class. I've tried adding an xslt param into the first template, but I've only managed to get various errors - nothing which actually works.
OK - position() is a tricky bastard, so here's a mandatory exercise:
1. Add a <xsl:value-of select="position()" /> inside the "Image" template and see what you get... OK? 11111
2. Now move the value-of instruction into the "*" template and see what that gets you... OK? 12345
The value of position() is evaluated in the "current set" which roughly translates to the apply-templates context, and the "Image" template is applied 5 times with a set of one node, whereas the "*" template is applied a single time with a set of 5 nodes.
Chriztian, thanks again, that's worked a treat. I was fumbling along the right lines but as you mentioned in your last post I had the position() in the wrong template, so it was always passing through 1. Thank you!
Template to iterate through media items
Hi,
I have a very simple gallery of images on my site, using the new schema (4.6.1). Because this is such a basic site and there will only ever be a maximum of 5 images in the gallery (there may be fewer) and I want the UI to be as utterly simple as possible, I've decided to add 5 'media picker' data types to the gallery document type, called mainImage1, mainImage2, mainImage3, mainImage4 and mainImage5 rather than adding a container to create 'nodes' of images in a folder as I'd normally do.
Whilst this is the simplest approach from a site admin point of view, I'm struggling with getting the most efficient XSLT to iterate through each of these image data types without repeating the code for each media item.
This code shows the first image, but rather than repeat this whole bit of code 5 times, I'd prefer to use some kind of loop/template mechanism just to iterate over this code 5 times. Make sense?
Can anyone suggest how to template this such that I don't have to repeat the code?
My other option is to use the 'Embedded Content' package, but I'd rather not go down the package route if possible.
Thanks for any pointers...
Hi Dan,
Here's a clean XSLT way of doing just that - ask away if you're curious:
/Chriztian
Spot on! Thanks Chriztian, that's really really useful.
Hi Chriztian, how would I get the alt attribute of the rendered image to be the number (position()) of that image? So I'd end up with:
Actually, what I'm trying to do is give the first image a particular class, but the other images no class. I've tried adding an xslt param into the first template, but I've only managed to get various errors - nothing which actually works.
Any ideas?
Thanks
Hi Dan,
Oh man - sadly this will be the short version (the ******* thing ate my very long and detailed explanation):
You should change the templates for "*" and "Image" to something like this:
/Chriztian
OK - position() is a tricky bastard, so here's a mandatory exercise:
1. Add a <xsl:value-of select="position()" /> inside the "Image" template and see what you get... OK? 11111
2. Now move the value-of instruction into the "*" template and see what that gets you... OK? 12345
The value of position() is evaluated in the "current set" which roughly translates to the apply-templates context, and the "Image" template is applied 5 times with a set of one node, whereas the "*" template is applied a single time with a set of 5 nodes.
/Chriztian
Chriztian, thanks again, that's worked a treat. I was fumbling along the right lines but as you mentioned in your last post I had the position() in the wrong template, so it was always passing through 1. Thank you!
is working on a reply...