If you want to simply pull the color from the homepage, you can use:
<xsl:variable name="homepage" select="$currentPage/ancestor-or-self::Homepage [@isDoc]" /> <!-- find the homepage by walking up the tree, and store in a variable --> Color: <xsl:value-of select="$homepage/yourColorPropertyAlias" />
If you want to use a true "recursive" function, meaning you can override the color on textpages, or if left blank it will revert to it's closest parent that has one specified, you can use:
<xsl:variable name="recursiveColor" select="$currentPage/ancestor-or-self::*[normalize-space(yourColorPropertyAlias)][1]/yourColorPropertyAlias" /> <!-- walks up the tree until it finds a value specified for yourColorPropertyAlias and returns the closest one --> Recursive color: <xsl:value-of select="$recursiveColor"/>
You also have the option of using Advanced Macro Parameter Syntax. Using this you can create a Parameter on your macro called myColor, and pass in the value through the template, using AMPS to handle the recursion for you.
In your template (make sure you first add the parameter to your Macro in the Developer -> Macros section):
<umbraco:Macro Alias="YourMacro" runat="server" myColor="[$yourColorPropertyAlias]" /> <!-- the $ tells Umbraco to retrieve the property recursively -->
Tom. I can see that there are three option you have given. I'm really new to all this and am not sure which is best to try.
If I said that the umbraco installation is host to three websites/homepages. And each home page can select their own colour then which way should I go with.
If you want to make the color "override-able" on the textpages, then use one of the two latter methods. If you're always going to have the color chosen on the homepage and that will be the color throughout the site, just use the first one (although the others will still work as well).
No worries. I only mentioned the other two methods because using "recursive" values is pretty popular in Umbraco. For example, maybe you want all of your pages to show the same header image, but some pages should have their own unique one. This is a good candidate for using the recursive functionality - you specify the main image on your top node (usually your Homepage), and have an option on the subpages to override it. Using the recursive technique, your macro will walk up the content tree until it finds a node with an image specified, and use the closest one. So by default all pages will use the one specified on the home node, but if you specify a unique image on a given page, itself and it's children will show that instead.
Anyway, if that's not required just start with the first example which should be pretty simple. It uses the ancestor-or-self axis to walk up your content tree until it finds the homepage, and stores the homepage in a variable. Then you can access any property you want from it by using the variable, ie $homepage/siteTitle
Note I forgot to mention, in the first example change the string "Homepage" to whatever the alias of your Homepage's document type is.
Thanks for sticking with me on this one. It's late here now but I think it would be good if I took a day or two to figure out exactly what I'm having trouble with, like you said.
This should make it easier for me to clarify my problem to you and myself.
I really think it's quite simple for people like you but I am a novice and just want to get it as straight in my head as I can first.
So after you mentioned the recursive function I looked into it. I'd not heard of it before but can see that the recursive function is used in the simple starter kit to display the website name.
I have experimented a little and have worked out how to display text on all pages based on a field generated from the top document type in the hierachy.
Thanks for putting me on the right track with this. It has certainly got me started but I have a way to go.
I now want to take the next step. I have created a field in the top document type that is a media picker. I use it to pick a banner image.
My question is how do I display the banner on each page. I am able to display the ID number of the banner on each page, but don't know how to display the actual image itself.
Sorry for the delay in getting back to you. It sounds like your "recursive" functionality is working, the issue now is that the Media Picker stores the ID of the image you select. From there you need to use a helper function to get the actual Media Item, which you can then get the file path (stored in umbracoFile) from.
Normally you would do this using an XSLT Macro instead of the umbraco:Item tag. There's a good writeup of how to do this in an XSLT macro on Lee Kelleher's blog. Here is the gist, this gets your property recursively then shows the image:
<xsl:variable name="topHeader" select="$currentPage/ancestor-or-self::*[normalize-space(topHeader)][1]/topHeader" /> <!-- Grab topHeader value recursively --> <xsl:if test="$topHeader > 0"> <!-- Make sure we have a value - if we don't, GetMedia will throw an exception --> <xsl:variable name="media" select="umbraco.library:GetMedia($topHeader, false())" /> <!-- Gets the Media Item from the ID and stores in $media variable --> <xsl:if test="$media/umbracoFile"> <!-- Make sure the media item has a file and still exists --> <img src="{$media/umbracoFile}" class="someClass" /> <!-- Show the image --> </xsl:if> </xsl:if>
If you really want to use the umbraco:Item tag there is a way to do it, but it's messy and not reccomended:
How to display a string value from another page
I have two types of content.
One content type is the 'homepage', it's at the very top of the web hierarcy.
The other content type is a simple 'textpage'. Text pages are always sub pages of the hompage.
I want to be able to control the background colour of the text pages by drawing from a field (x) that is set in the 'homepage'.
I guess this must be easy for most but I am a complete novice at xslt and would be grateful for some guidance.
Cheers
Steve
Hi Steve,
If you want to simply pull the color from the homepage, you can use:
If you want to use a true "recursive" function, meaning you can override the color on textpages, or if left blank it will revert to it's closest parent that has one specified, you can use:
You also have the option of using Advanced Macro Parameter Syntax. Using this you can create a Parameter on your macro called myColor, and pass in the value through the template, using AMPS to handle the recursion for you.
In your template (make sure you first add the parameter to your Macro in the Developer -> Macros section):
And your macro:
Hope this helps,
Tom
Hi Tom,
Thanks for this quick reply. I'm going to see if I can work with your suggention. I'll let you know how I get on.
Many thanks.
STeve
Hi,
Tom. I can see that there are three option you have given. I'm really new to all this and am not sure which is best to try.
If I said that the umbraco installation is host to three websites/homepages. And each home page can select their own colour then which way should I go with.
Steve
Hi Steve,
If you want to make the color "override-able" on the textpages, then use one of the two latter methods. If you're always going to have the color chosen on the homepage and that will be the color throughout the site, just use the first one (although the others will still work as well).
-Tom
Hi Tom. Thanks. This is still a bit over my head. I'll give it some thought.
Cheers.
Steve
No worries. I only mentioned the other two methods because using "recursive" values is pretty popular in Umbraco. For example, maybe you want all of your pages to show the same header image, but some pages should have their own unique one. This is a good candidate for using the recursive functionality - you specify the main image on your top node (usually your Homepage), and have an option on the subpages to override it. Using the recursive technique, your macro will walk up the content tree until it finds a node with an image specified, and use the closest one. So by default all pages will use the one specified on the home node, but if you specify a unique image on a given page, itself and it's children will show that instead.
Anyway, if that's not required just start with the first example which should be pretty simple. It uses the ancestor-or-self axis to walk up your content tree until it finds the homepage, and stores the homepage in a variable. Then you can access any property you want from it by using the variable, ie $homepage/siteTitle
Note I forgot to mention, in the first example change the string "Homepage" to whatever the alias of your Homepage's document type is.
Feel free to ask if you get stuck :)
-Tom
Hi Tom,
Your description sounds exactly what I want to acheive.
But I have no idea how to do it.. Do you have an email address I could contact you on?
Steve
Tom. I was wondering if you might be interested in a small amount of freelance work? Just to edit my site to show a working example.
Steve
Hit me up on twitter and I'll see if I can help you out and show you a quick example :) @tomnf1
Also which part exactly are you having trouble with? I'm sure we can figure it out...
Hi Tom,
Thanks for sticking with me on this one. It's late here now but I think it would be good if I took a day or two to figure out exactly what I'm having trouble with, like you said.
This should make it easier for me to clarify my problem to you and myself.
I really think it's quite simple for people like you but I am a novice and just want to get it as straight in my head as I can first.
I'll get back to you in about two days.
Cheers
Steve
Hi Tom,
So after you mentioned the recursive function I looked into it. I'd not heard of it before but can see that the recursive function is used in the simple starter kit to display the website name.
I have experimented a little and have worked out how to display text on all pages based on a field generated from the top document type in the hierachy.
Thanks for putting me on the right track with this. It has certainly got me started but I have a way to go.
I now want to take the next step. I have created a field in the top document type that is a media picker. I use it to pick a banner image.
My question is how do I display the banner on each page. I am able to display the ID number of the banner on each page, but don't know how to display the actual image itself.
Any ideas?
Steve
Hello Again Tom,
I can see that you have previously posted responses to my question about displaying a media picker image.
I just don't seem to be able to would out how to adapt your method to be recursive.
So far i have <umbraco:item id="Item33" runat="server" field="topHeader" recursive="true"></umbraco:item>
But this just displayes the id number of the image.
Steve
Hello again Tom.
I don't think my last post was very clear.
I meant to say...
I just don't seem to be able to work out how to adapt your method to be recursive.
So far i have <umbraco:item id="Item33" runat="server" field="topHeader" recursive="true"></umbraco:item>
But this just displayes the id number of the image. Not the image itself.
Steve
Hi Steve,
Sorry for the delay in getting back to you. It sounds like your "recursive" functionality is working, the issue now is that the Media Picker stores the ID of the image you select. From there you need to use a helper function to get the actual Media Item, which you can then get the file path (stored in umbracoFile) from.
Normally you would do this using an XSLT Macro instead of the umbraco:Item tag. There's a good writeup of how to do this in an XSLT macro on Lee Kelleher's blog. Here is the gist, this gets your property recursively then shows the image:
If you really want to use the umbraco:Item tag there is a way to do it, but it's messy and not reccomended:
Hope this helps,
Tom
Brilliant. That worked. Thanks a lot Tom.
is working on a reply...