I cant believe something that should be so simple is taking so much time already. Im quite frustrated so please bear with me.
I have a documenttype product which has a property 'image' which i want to show in my Product template, so directly into my masterpage.
I have tried numerous of variants i found on the forums but none of them work.
<umbraco:Item runat="server" field="groteAfbeelding" /> this results in 1091 (id of the media item) But it seems impossible to actually retreive the actual path of the image.
Please note that i have been trying to get this done for about an hour, trying all sorts different solutions i found on the forum. The fact that i can find so many different solutions should also make clear that something is wrong.
Even when i hardcode the id of the media item it doenst work:
I dont receive an error, it just return nothing.
I created the masterpages myself, using Visual Studio, am i missing a reference or something?
Eventually i got this working by creating an xslt macro which shows images using with the id which is passed in a parameter. This works, but it seems very weird.
I would still like to know, how i would do this in my masterpage directly.
The reason you can't "just" output the image is because that's not always what you want - sometimes you want an <img> tag, other times you may want just the URL for use in a data-src attribute, or within a JavaScript, etc.
Umbraco provides the GetMedia() extension for getting the XML which describes the media item, so you have access to the path, width, height etc. - everything you need to build an <img> tag, calculate the number of pixels, or any other thing you may want to do, which an XSLT Macro or the Razor equivalent is perfect for.
As an example, I usually have a "PageImage" macro that handles all the nitty-gritty of making sure that there's actually still an image file stored for the Media item ID that was saved in a page's "pageImage" property (this happens all the time when external editors are involved). It also handles cropping stuff (if needed) etc. - the main point is that my masterpage just has the following line where I want the image:
That line explains som much better what's going on, than any attempt to cram all the info needed into a single call there (my own opinion, of course).
I know there are people working on an <umbraco:Media ...> server tag that could help in some situations, but the macro "middleman" is an excellent way to not have your masterpage actually handle the media stuff, but rather say "this is where I want you to put the image" in stead.
Hope it helps you understand why this works this way.
Thank you for taking the time to explain this to me. I was rather frustrated as you might have noticed.
The part that caused this frustration is that i was able to get to the actual data of the image, but i was not able to output just umbracoFile value of the image field in the masterpage. So i got the xml data of the string but was unable to query it. I would suggest not even giving me that, just the id of the image, so it would be a bit more obvious that i would have to think of a different solution. Now I just kept trying to get it right. The funny thing is that the forums gave me all kinds solutions which also didnt work, is that an issue with the version? That gave me the idea that it was supposed to work this way, which apparantly isnt.
In the end i came up with the exact same solution you did, use a macro.
Anyway thank for your reply, it is very much appreciated.
Yeah, unfortunately there's a lot of code on the forums that doesn't work unless you have the exact same scenario as the original poster, reasons include:
* Different types of input parameters, i.e., a macro mediaCurrent parameter gives you the XML, whereas a mediaPicker only stores the ID - this needs two different approaches when extracting the path.
* Different versions of Umbraco, i.e., versions prior to 4.5 used a different (and very clumsy) XML Schema (now referred to as the "Legacy Schema") where you'd have to be very careful when writing even simple XPath expressions - that was improved significantly with 4.5 - but then there was a silly little bug in the GetMedia() extension, which wasn't fixed until 4.5.2 (@Admins: Please correct me if I'm wrong).
Hopefully, we'll manage to solve these ambiguities with the Umbraco Documentation project on GitHub.
Showing an image using getmedia in template
I cant believe something that should be so simple is taking so much time already.
Im quite frustrated so please bear with me.
I have a documenttype product which has a property 'image' which i want to show in my Product template, so directly into my masterpage.
I have tried numerous of variants i found on the forums but none of them work.
<umbraco:Item runat="server" field="groteAfbeelding" /> this results in 1091 (id of the media item)
But it seems impossible to actually retreive the actual path of the image.
Please note that i have been trying to get this done for about an hour, trying all sorts different solutions i found on the forum. The fact that i can find so many different solutions should also make clear that something is wrong.
If you are trying to get the url of an image from the media section assuming you are using a media Picker you can either make use of razor or xslt.
Try this
Hope it helps
Thanks for your quick reply, but it doesnt work.
Even when i hardcode the id of the media item it doenst work:
I dont receive an error, it just return nothing.
I created the masterpages myself, using Visual Studio, am i missing a reference or something?
Eventually i got this working by creating an xslt macro which shows images using with the id which is passed in a parameter.
This works, but it seems very weird.
I would still like to know, how i would do this in my masterpage directly.
Hi Bunnynut - welcome to the forum!
The reason you can't "just" output the image is because that's not always what you want - sometimes you want an <img> tag, other times you may want just the URL for use in a data-src attribute, or within a JavaScript, etc.
Umbraco provides the GetMedia() extension for getting the XML which describes the media item, so you have access to the path, width, height etc. - everything you need to build an <img> tag, calculate the number of pixels, or any other thing you may want to do, which an XSLT Macro or the Razor equivalent is perfect for.
As an example, I usually have a "PageImage" macro that handles all the nitty-gritty of making sure that there's actually still an image file stored for the Media item ID that was saved in a page's "pageImage" property (this happens all the time when external editors are involved). It also handles cropping stuff (if needed) etc. - the main point is that my masterpage just has the following line where I want the image:
That line explains som much better what's going on, than any attempt to cram all the info needed into a single call there (my own opinion, of course).
I know there are people working on an <umbraco:Media ...> server tag that could help in some situations, but the macro "middleman" is an excellent way to not have your masterpage actually handle the media stuff, but rather say "this is where I want you to put the image" in stead.
Hope it helps you understand why this works this way.
/Chriztian
Hi Chriztian,
Thank you for taking the time to explain this to me. I was rather frustrated as you might have noticed.
The part that caused this frustration is that i was able to get to the actual data of the image, but i was not able to output just umbracoFile value of the image field in the masterpage. So i got the xml data of the string but was unable to query it.
I would suggest not even giving me that, just the id of the image, so it would be a bit more obvious that i would have to think of a different solution.
Now I just kept trying to get it right. The funny thing is that the forums gave me all kinds solutions which also didnt work, is that an issue with the version?
That gave me the idea that it was supposed to work this way, which apparantly isnt.
In the end i came up with the exact same solution you did, use a macro.
Anyway thank for your reply, it is very much appreciated.
Bas
Hi Bas - you're welcome!
Yeah, unfortunately there's a lot of code on the forums that doesn't work unless you have the exact same scenario as the original poster, reasons include:
* Different types of input parameters, i.e., a macro mediaCurrent parameter gives you the XML, whereas a mediaPicker only stores the ID - this needs two different approaches when extracting the path.
* Different versions of Umbraco, i.e., versions prior to 4.5 used a different (and very clumsy) XML Schema (now referred to as the "Legacy Schema") where you'd have to be very careful when writing even simple XPath expressions - that was improved significantly with 4.5 - but then there was a silly little bug in the GetMedia() extension, which wasn't fixed until 4.5.2 (@Admins: Please correct me if I'm wrong).
Hopefully, we'll manage to solve these ambiguities with the Umbraco Documentation project on GitHub.
/Chriztian
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.