How to select and display an image associated with an entry
This is an open discussion with 9 replies, filed under XSLT.
Search
Could you post your xsl file, so we can see how your manipulating the xml data? .use pastie.org to post your xsl
From looking at your xml above, im thinking you need to associate the image entry ID with the article ID and iterate over the xml in an apply-template method to grab all images linked to the article... Im on a mobile so cant post code right now !
I want the page to show a list of each entry within a category - the title, the description, and the cover image associated with it (I have two types of image, cover and album)
So I am using a for:each to iterate through the entries that are output by the data-source, and to display the title and the description... so far, so good.
<xsl:for-each select="entries/entry"> <h3><a href="{$root}/portfolio/entry/{title/@handle}/"><xsl:value-of select="title"/></a></h3> <xsl:copy-of select="description/*"/> </xsl:for-each>
I would like to add a line or two that then display the image associated with each entry:
<div class="categorypic"><a href="{$root}/workspace/images/{/data/category-image/entry[@id=***]/image/filename}"><img src="{$root}/image/1/0/100/images/{/data/category-image/entry[@id=***]/image/filename}" /></a></div>
I know this is not the way to do it, but I want the *** to reflect the id of the entry that is being cycled through.
I have only ever used php before, so this may be the wrong angle to come at the problem using XSLT.
Thanks for your help. Alex
Try something like this:
<div class="categorypic"><a href="{$root}/workspace/images/{/data/category-image/entry[@id=current()/@id]/image/filename}"><img src="{$root}/image/1/0/100/images/{/data/category-image/entry[@id=current()/@id]/image/filename}" /></a></div>
I think that's right, if you are in the entry node of the xml that is.
I am in the entry node as far as I'm aware (that's set by the for:each function right?)
That hasn't worked... I've checked my paths, and they are all correct, it is merely the entry[@id=***] that doesn't seem to be firing.
I have also tried entry[@id=@id], and entry[@id='entry/@id'], but to no avail.
Apologies for the double post... it seems that this half works:
<div class="categorypic"><a href="{$root}/workspace/images/{/data/category-images/entry[@id=./@id]/image/filename}"><img src="{$root}/image/1/0/100/images/{/data/category-images/entry[@id=./@id]/image/filename}" /></a></div>
but the ./@id only selects the first one, not a different image for each iteration.
*** edit ***
it seems that ./ selects the context node, but I am not sure what that is, or how it differs from the current node
I know it is the height of bad form to keep posting, but I feel that this could be of use to others with similar issues.
Thank you very much to @moonoo2 as without "current()" I would never have got this working.
The line of code below now works, although it is mighty ugly!
<div class="categorypic"><a href="{$root}/workspace/images/{/data/category-images/entry/entry/item[@id=current()/@id]/../../image/filename}"><img src="{$root}/image/1/100/0/images/{/data/category-images/entry/entry/item[@id=current()/@id]/../../image/filename}" /></a></div>
I realised that the filename I was referencing in the XML was not in the same node as the entry/item[@id] that I was selecting it with - hence having to climb back up the structure a couple of levels.
Phew!
@alexdonal: this gives you another way of working with templates which might mean you have to do less ugly stuff in XSLT: A xsl:apply-templates Tutorial from Allen Chang
@DavidOliver: that is a great explanation, thank you. I was struggling to get my head round apply-templates.
I think the main reason it looks ugly though, is the select-box-link field that associates an image with an entry seems to be buried in the XML the data-source spits out, and I have to climb back out a couple of levels to go back in to get the information I require.
Have I set up my data-sources incorrectly do you think?
Create an account or sign in to comment.
Good day (or morning, or night, depending on where you are),
First off, Hello, I'm new here and Symphony is amazing! I have struggled through many different CMS options before I finally found this one, and I wish I had found it earlier.
However, I am struggling with displaying an image linked to an entry. I wish to display a list of all the entries in a category, with a thumbnail next to the title.
I have managed to get all the images associated with a single entry to display, but multiple ones elude me.
Could anyone point me in the direction of a good tutorial, or possibly help?! (I have tried searching both here and the web, but to no avail, apologies if I have missed anything)
So my images come out like this in the XML, and my entries come out like this:
My Website if seeing it helps?
Thanks in advance for any help
Alex