Copied to clipboard

Flag this post as spam?

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


  • Andrei Canef 33 posts 84 karma points
    Mar 14, 2011 @ 16:54
    Andrei Canef
    0

    passing a node ID as a parameter to another template

    Hi there.

    I don't have much experience with XSLT, so I'm sure the answer to this will be something quite obvious that I've missed.

    I have an XSLT macro that returns an image with a given ID.

    I created a Document Type with 3 fields: an image picker that points to an image in a subfolder within my media folder, a url and a title. I want to recursively get the image for each object of that document type, wrap it in a link pointing to the object url, and put the title as the alt tag for the image.

     

    As such, I wrote a second macro that is meant to generate the link markup and call the first macro for every object in my collection.

     

    However, that doesn't seem to work. I am unsure whether I am passing the image ID incorrectly to the getImagebyID macro, or whether there's an error in my code.

     

    I've set "breakpoints" via comments, and the main loop executes fine - it sets the urls in the href. The images refuse to appear, however, so I assume I'm either not passing the node ID correctly, or that the error is within the getLogoById function itself.

     

    Thanks for the input.

     

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:include href="getLogoById.xslt"/>
        <xsl:param name="param"/>

    <xsl:template match="/">
      <xsl:variable name="sponsorLogos" select="$currentPage/ancestor::root/homepage/SponsorList//* [@isDoc]" />
       <xsl:if test="count($sponsorLogos) &gt; 0">
       <xsl:for-each select="$sponsorLogos">
                 <li>
                   <a>
                     <xsl:attribute name="href">
                       <xsl:value-of select="sponsorLink"/>
                     </xsl:attribute>
                     <xsl:copy-of select="logo"/>
                       <xsl:call-template name="getLogo">
                         <xsl:with-param name="param">
                           <xsl:value-of select="logo"/>
                         </xsl:with-param>
                     </xsl:call-template>
                     </a>
              </li>
         </xsl:for-each>
      </xsl:if>

    </xsl:template>

    </xsl:stylesheet>

    That's the main template, and here's getLogoById

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template name="getLogo" match="/">
        <xsl:param name="param"/>
      <xsl:copy-of select="$param"/>

      <xsl:if test="$param > 0">
      
        <xsl:copy-of select="$param"/>
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($param, 0)" />
            <xsl:if test="$param/umbracoFile">
                <img>
              <xsl:value-of select="umbraco.library:GetMedia($param, 'false')/data [@alias = 'umbracoFile']"/>
              <xsl:attribute name="alt">
                      <xsl:value-of select="@nodeName" disable-output-escaping="yes"/>
                    </xsl:attribute>
        </img>
            </xsl:if>
        </xsl:if>
    </xsl:template>

    </xsl:stylesheet>
  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Mar 14, 2011 @ 21:23
    Chriztian Steinmeier
    0

    Hi Andrei,

    You're mixing the XML formats in your include file - ($param/umbracoFile vs. data[@alias = 'umbracoFile']).

    Try this, to see how templates can do a lot for you in XSLT:

    Macro:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:umbraco.library="urn:umbraco.library"
            exclude-result-prefixes="umbraco.library"
    >
    
            <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
            <xsl:param name="currentPage" />
            <xsl:variable name="homePage" select="$currentPage/ancestor-or-self::root/homepage" />
    
            <xsl:template match="/">
                    <xsl:apply-templates select="$homePage/SponsorList//*[@isDoc]" />
            </xsl:template>
    
            <xsl:template match="*[@isDoc]">
                    <li>
                            <a href="{sponsorLink}">
                                    <xsl:apply-templates select="logo" mode="media" />
                            </a>
                    </li>
            </xsl:template>
    
            <xsl:include href="getLogoById.xslt" />
    
    </xsl:stylesheet>

    Include:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:umbraco.library="urn:umbraco.library"
            exclude-result-prefixes="umbraco.library"
    >
    
            <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
            <xsl:template match="/" name="getLogo">
                    <xsl:param name="param" />
                    <xsl:apply-templates select="$param" mode="media" />
            </xsl:template>
    
            <xsl:template match="*" mode="media">
                    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(., false())" />
                    <!-- Process media if it exists -->
                    <xsl:apply-templates select="$mediaNode[not(error)]" />
            </xsl:template>
    
            <!-- Template for Image media -->
            <xsl:template match="Image">
                    <img src="{umbracoFile}" alt="{@nodeName}" />
            </xsl:template>
    
            <!-- Template for Folder media -->
            <xsl:template match="Folder">
                    <!-- do stuff -->
            </xsl:template>
    
            <!-- Catch empty fields called in media mode -->
            <xsl:template match="*[not(normalize-space())]" mode="media">
                    <!-- Empty template - do not try to GetMedia() on this -->
            </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

  • Andrei Canef 33 posts 84 karma points
    Mar 15, 2011 @ 10:37
    Andrei Canef
    0

    Hi Chriztian, 

     

    Thanks for the reply. 

     

    I am a bit confused by a few things though: 

     <xsl:appl name="sponsorLogos" select="$homePage/SponsorList//*[@isDoc]" />

    I assume that's meant to say apply-template, but what is the point of applying the same template twice? 
    Also, how is the image returned, exactly? I see that in your include, the macro selects the required node, but should that not be within umbraco tags, to actually render the image?  

    And finally, for some reason, the main macro isn't saving at all(neither is the main macro, but I assume that was because of the fact that it was expecting an unexisting parameter on the media mode template). The error I'm getting is Error occured

    Error in XSLT at line 11, char 10
    9:       <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    10:      
    11: >>>   <xsl:param name="currentPage" /> <<<
    12:       <xsl:variable name="homePage" select="$currentPage/ancestor-or-self::root/homepage" />

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Mar 15, 2011 @ 12:59
    Chriztian Steinmeier
    0

    Hi Andrei, 

    Oops - sorry! I've updated the code above (the <xsl:appl name... /> should just be deleted. As should the currentPage parameter from the include).

    /Chriztian

  • Andrei Canef 33 posts 84 karma points
    Mar 15, 2011 @ 13:09
    Andrei Canef
    0

    That worked, AND cleared things up a bit at the same time. 

     

    Thank you for the help:)

     

    Andrei

Please Sign in or register to post replies

Write your reply to:

Draft