Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Stephen 47 posts 69 karma points
    Mar 29, 2012 @ 14:26
    Stephen
    0

    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

     

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 29, 2012 @ 14:50
    Tom Fulton
    2

    Hi Steve,

    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 -->

    And your macro:

    <xsl:param name="myColor" select="/macro/myColor"/>
    ...
    <xsl:template match="/">
    Color passed in:  <xsl:value-of select="$myColor"/>
    </xsl:template>

    Hope this helps,
    Tom

  • Stephen 47 posts 69 karma points
    Mar 29, 2012 @ 15:00
    Stephen
    0

    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

  • Stephen 47 posts 69 karma points
    Mar 29, 2012 @ 15:08
    Stephen
    0

    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

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 29, 2012 @ 15:11
    Tom Fulton
    0

    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

  • Stephen 47 posts 69 karma points
    Mar 29, 2012 @ 15:13
    Stephen
    0

    Hi Tom. Thanks. This is still a bit over my head. I'll give it some thought.

    Cheers.

     

    Steve

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 29, 2012 @ 15:18
    Tom Fulton
    0

    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

  • Steve 6 posts 26 karma points
    Mar 29, 2012 @ 17:46
    Steve
    0

    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

     

  • Steve 6 posts 26 karma points
    Mar 29, 2012 @ 20:49
    Steve
    0

    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

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 29, 2012 @ 21:10
    Tom Fulton
    0

    Hit me up on twitter and I'll see if I can help you out and show you a quick example :)  @tomnf1

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 29, 2012 @ 22:46
    Tom Fulton
    0

    Also which part exactly are you having trouble with?  I'm sure we can figure it out...

  • Steve 6 posts 26 karma points
    Mar 29, 2012 @ 23:20
    Steve
    0

    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

  • Steve 6 posts 26 karma points
    Mar 31, 2012 @ 14:18
    Steve
    0

    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

  • Steve 6 posts 26 karma points
    Mar 31, 2012 @ 14:33
    Steve
    0

    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

  • Stephen 47 posts 69 karma points
    Apr 02, 2012 @ 14:35
    Stephen
    0

    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

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 03, 2012 @ 16:17
    Tom Fulton
    0

    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:

    <xsl:variable name="topHeader" select="$currentPage/ancestor-or-self::*[normalize-space(topHeader)][1]/topHeader" />  <!-- Grab topHeader value recursively -->
    <xsl:if test="$topHeader &gt; 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:

    <umbraco:Item runat="server" field="topHeader" xslt="concat('&lt;img src=&quot;',umbraco.library:GetMedia({0}, false())/umbracoFile, '&quot; /&gt;')" xsltDisableEscaping="true"/>

    Hope this helps,
    Tom

  • Stephen 47 posts 69 karma points
    Apr 03, 2012 @ 16:24
    Stephen
    0

    Brilliant. That worked. Thanks a lot Tom.

     

Please Sign in or register to post replies

Write your reply to:

Draft