GetImage using a macro parameter with a doctype parameter name as input?
I'm trying to implement some form of generic, recursive image rendering script that takes the alias of a doctype parameter as a macro parameter. It should then display the image that has been picked using a media picker with that alias name. Preferably recursive.
Here's the setup so far:
I have an XSLT with a corresponding macro. The macro parameter contains the alias of the doctype property that I want to get the image from.
So if my doctype has a property (media picker) with the alias "footerImage" the macro in the template looks like this:
So if get the value of that variable i get "footerImage".
But then things start to get complicated. I want to get the media that has been selected with the doctype propery with the name "footerImage". I try this, but it doesn't work:
It's perfectly doable - you can use the name() funtion to accomplish this, e.g.:
<xsl:variable name="imageProperty" select="$currentPage/*[name() = /macro/aliasName]" />
...
<xsl:if test="normalize-space($imageProperty)">
<!-- Do the GetImage() thing -->
</xsl:if>
/Chriztian
BTW: If you're looking for something that can handle the DAMP picker, cropping and overriding sizes etc., maybe have a look at my MediaHelper on GitHub...
...I get the name of the alias printed to the screen, so the parameter has a value. I have also checked that the doctype behind the template where the macro is added has a parameter with the name that I pass to the macro.
I know what's wrong now - I forgot about the context when I used the shortcut of referencing /macro/aliasName. We need to store that first and then use it subsequently - this should actually work:
<!-- Grab the name of the property to look for -->
<xsl:variable name="propertyName" select="/macro/aliasName"/>
<!-- Recursively grab the property (first one that has a value in it) -->
<xsl:variable name="image" select="$currentPage/ancestor-or-self::*[normalize-space(*[name() = $propertyName])][1]/*[name() = $propertyName]" />
<!-- Use the value... -->
<xsl:value-of select="$image" />
Thanks a million for your time and knowledge Chriztian!
Your example above works just like I want it to! Now the script works recursively and I can put the macro in either a master template or a "child template". When I run this and the property I'm supplying as a macro parameter is a media picker I get the id of the media node (in this case an image), for example 1064. Now I can go on and use this in GetMedia just like I would normally do.
GetImage using a macro parameter with a doctype parameter name as input?
I'm trying to implement some form of generic, recursive image rendering script that takes the alias of a doctype parameter as a macro parameter. It should then display the image that has been picked using a media picker with that alias name. Preferably recursive.
Here's the setup so far:
I have an XSLT with a corresponding macro. The macro parameter contains the alias of the doctype property that I want to get the image from.
So if my doctype has a property (media picker) with the alias "footerImage" the macro in the template looks like this:
Then I have the XSLT showImage.xslt where I get the value from the parameter:
So if get the value of that variable i get "footerImage".
But then things start to get complicated. I want to get the media that has been selected with the doctype propery with the name "footerImage". I try this, but it doesn't work:
But only this far into my quest I get an XSLT-error. And then I haven't even started with GetMedia...
Is what I want to to doable with XSLT? If not, what's the best way to get a generic XSLT-that fetches images?
Hi Thomas,
It's perfectly doable - you can use the name() funtion to accomplish this, e.g.:
/Chriztian
BTW: If you're looking for something that can handle the DAMP picker, cropping and overriding sizes etc., maybe have a look at my MediaHelper on GitHub...
Hmm, it doesn't seem to work for me. I tried:
I get nothing printed to the page. But If I change it to:
...I get the name of the alias printed to the screen, so the parameter has a value. I have also checked that the doctype behind the template where the macro is added has a parameter with the name that I pass to the macro.
Hi Thomas,
Okay - that sounds a little weird, but let's find out what's going on...
So you know that
$currentPage
has a property with whatever alias you're sending into the aliasName parameter, right?What version of Umbraco are you using? Do you know if you're using the "legacy" schema or the new (since 4.5) one?
Try this to do some debugging:
and let us know what you get...
/Chriztian
Hi!
I'm using Umbraco 7.0.4 and the new schema and I get these values when I run your diagnostic script.
$imageProperty is empty.
$imagePropertyName is:
Macro parameters is:
Worth noting is that in the end I want to do this recursively, but that changes nothing when I try it. Like this:
This should get the value of the property regardless of which page it's on.
Hi Thomas,
I know what's wrong now - I forgot about the context when I used the shortcut of referencing /macro/aliasName. We need to store that first and then use it subsequently - this should actually work:
/Chriztian
Thanks a million for your time and knowledge Chriztian!
Your example above works just like I want it to!
Now the script works recursively and I can put the macro in either a master template or a "child template". When I run this and the property I'm supplying as a macro parameter is a media picker I get the id of the media node (in this case an image), for example 1064. Now I can go on and use this in GetMedia just like I would normally do.
/Thomas
is working on a reply...