Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1284 posts 2741 karma points
    Mar 28, 2011 @ 21:33
    Amir Khan
    0

    Recursive Media Macro

    Hi,

    I'm using a macro to put an image in my template with the new Schema. How could I make it so it is recursive incase someone doesn't choose an image?

    My Code is as follows:

    <xsl:param name="currentPage"/>


    <xsl:template match="/">
      <xsl:variable name="mediaId" select="number($currentPage/headerImage)" />
      <xsl:if test="$mediaId > 0">
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
        <xsl:if test="$mediaNode/umbracoFile">
          <img src="{$mediaNode/umbracoFile}" alt="{$mediaNode/@nodeName}" height="{$mediaNode/umbracoHeight}" width="{$mediaNode/umbracoWidth}" />
        </xsl:if>
      </xsl:if>
    </xsl:template>

    I've tried things like this but am not sure if it is the old schema or I'm just doing something wrong:

    <xsl:variable name="logoid" select="$currentPage/ancestor-or-self::node[data[@alias = 'headerImage']][1]/data[@alias='headerImage']" />

    Thanks for any help!

    Amir

  • Kim Andersen 1447 posts 2196 karma points MVP
    Mar 28, 2011 @ 22:15
    Kim Andersen
    0

    Hi Amir

    Try taking a look at Chriztian's answer in this post.

    By using that solution I think you could do something like this:

    <xsl:variable name="mediaId" select="$currentPage/ancestor-or-self::*[normalize-space(headerImage)][1]/headerImage" />
      <xsl:if test="$mediaId > 0">
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
        <xsl:if test="$mediaNode/umbracoFile">
          <img src="{$mediaNode/umbracoFile}" alt="{$mediaNode/@nodeName}" height="{$mediaNode/umbracoHeight}" width="{$mediaNode/umbracoWidth}" />
        </xsl:if>
      </xsl:if>

    /Kim A

  • Amir Khan 1284 posts 2741 karma points
    Mar 28, 2011 @ 23:29
    Amir Khan
    0

    Kim,

    That worked great! What is the reason for the normalize-space in there? Not sure I've seen that before.

    Thank you for your help,

    Amir

  • Srdjan Lukic 34 posts 166 karma points
    Mar 29, 2011 @ 00:05
    Srdjan Lukic
    1

    Hi Amir 

    The normalize-space is used just in case the headerImage is a normal text string where user can type the ID, in that care the user will be able to type a space in the field wich will conflict with XSLT "like" function. So normalize-space is removing all space after and before the ID.  But if you are using a mediaPicker then it is not that needed.

     

     

  • Kim Andersen 1447 posts 2196 karma points MVP
    Mar 30, 2011 @ 08:35
    Kim Andersen
    0

    Hi Amir

    Yeah, like Srdjan says the normalize-space will strip all spaces in eny given string. And in the use of a media picker you might not need it (haven't tested that, but it makes sense).

    You can use the normalize-space in other situations like if you have a string like this:

    <xsl:variable name="ourString">This is a string</xsl:variable>
    <xsl:value-of select="normalize-space($ourString)" />

    The above would output: Thisisastring

    Can be usefull in situations where you might want to use some text (with spaces) as a class on a div or something like that.

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft