Copied to clipboard

Flag this post as spam?

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


  • Thomas Kahn 602 posts 506 karma points
    Feb 18, 2014 @ 11:12
    Thomas Kahn
    0

    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:

    <umbraco:Macro aliasName="footerImage" Alias="ShowImage" runat="server"></umbraco:Macro>

    Then I have the XSLT showImage.xslt where I get the value from the parameter:

    <xsl:variable name="aliasName" select="/macro/aliasName"/>

    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:

    <xsl:if test="$aliasName != ''">
    <xsl:value-of select="$currentPage/$aliasName"/>
    </xsl:if>

    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?

     

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 18, 2014 @ 11:17
    Chriztian Steinmeier
    0

    Hi Thomas,

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

  • Thomas Kahn 602 posts 506 karma points
    Feb 18, 2014 @ 11:32
    Thomas Kahn
    0

    Hmm, it doesn't seem to work for me. I tried:

    <xsl:variable name="image" select="$currentPage/*[name() = /macro/aliasName]"/>
    <xsl:value-of select="$image" />

    I get nothing printed to the page. But If I change it to:

    <xsl:variable name="image" select="/macro/aliasName"/>
    <xsl:value-of select="$image" />

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

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 18, 2014 @ 11:54
    Chriztian Steinmeier
    0

    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:

    <xsl:template match="/">
        <xsl:variable name="imageProperty" select="$currentPage/*[name() = /macro/aliasName]" />
        <xsl:variable name="imagePropertyName" select="/macro/aliasName" />
    
        <h2>$imageProperty</h2>
        <textarea>
            <xsl:copy-of select="$imageProperty" />
        </textarea>
    
        <h2>$imagePropertyName</h2>
        <textarea>
            <xsl:copy-of select="$imagePropertyName" />
        </textarea>
    
        <h2>Macro parameters</h2>
        <textarea>
            <xsl:copy-of select="/macro" />
        </textarea>
    
        <h2>Schema</h2>
        <p>
            <xsl:if test="$currentPage[self::node]">Legacy Schema</xsl:if>
            <xsl:if test="$currentPage[@isDoc]">New Schema</xsl:if>
        </p>
    
    </xsl:template>
    

    and let us know what you get...

    /Chriztian

  • Thomas Kahn 602 posts 506 karma points
    Feb 18, 2014 @ 13:12
    Thomas Kahn
    0

    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:

    <aliasname>footerBild1</aliasname>

    Macro parameters is:

    <macro><aliasname>footerBild1</aliasname></macro>

     Worth noting is that in the end I want to do this recursively, but that changes nothing when I try it. Like this:

     <xsl:variable name="imageProperty" select="$currentPage/ancestor-or-self::*[name() = string(/macro/aliasName)]" />

    This should get the value of the property regardless of which page it's on.

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 18, 2014 @ 13:45
    Chriztian Steinmeier
    100

    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:

    <!-- 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" />
    

    /Chriztian

  • Thomas Kahn 602 posts 506 karma points
    Feb 18, 2014 @ 14:34
    Thomas Kahn
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft