Copied to clipboard

Flag this post as spam?

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


  • Eric Herlitz 97 posts 129 karma points
    Mar 22, 2011 @ 18:11
    Eric Herlitz
    0

    Dynamic path in xslt

    Hi

    Im trying to figure out a way to use a dynamic path in my expressions

    <xsl:template match="/">
    <
    xsl:variable name="imagePosition" select="/macro/position" />      

      <xsl:if test="$currentPage/$imagePosition!='' and number($currentPage/$imagePosition)!=0">
          <xsl:variable name="file" select="umbraco.library:GetMedia(number($currentPage/$imagePosition),'false')"/>
          <xsl:if test="not($file/error)">
              <img src="{$file/umbracoFile}"/>
          </xsl:if>
      </xsl:if>
    </xsl:template>

     

    But ofcourse it fails misserably. Is there an eval of some sort?

    Thanks for any input

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 22, 2011 @ 18:24
    Tom Fulton
    0

    Hi,

    Not sure exactly what you mean by dynamic path, but one problem with the above is you should just be referencing $imagePosition, not $currentPage/$imagePosition (assuming you are wanting to reference the variable you defined and not a field on the page)

    -Tom

  • Daniel Bardi 927 posts 2562 karma points
    Mar 22, 2011 @ 18:56
    Daniel Bardi
    0

    Looks like $imagePosition is the media item's id so Tom's solution will fix it. 

    xslt variables are accessed by themselves ($imagePosition)

    page properties are accessed as children of the current page ($currentPage/propertyName)

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 22, 2011 @ 21:24
    Chriztian Steinmeier
    1

    Hi Eric,

    I'm guessing you're trying to name the property that holds the mediaId - so you'll need to use the name() function - here's a slightly rewritten sample to get you going:

    <xsl:template match="/">
        <!-- Grab the property right away - will not throw exception -->
        <xsl:variable name="imagePosition" select="$currentPage/*[name() = /macro/position]" />
        <!-- If there was a value -->
        <xsl:if test="normalize-space($imagePosition) ">
            <xsl:variable name="file" select="umbraco.library:GetMedia($imagePosition, false())" />
            <!-- Handle file, whether Image, File, Folder or whatever -->
            <xsl:apply-templates select="$file[not(error)]" />
        </xsl:if>
    </xsl:template>
    
    <!-- Template for Image -->
    <xsl:template match="Image">
        <img src="{umbracoFile}" width="{umbracoWidth}" height="{umbracoHeight}" alt="{@nodeName}" />
    </xsl:template>

    /Chriztian 

  • Daniel Bardi 927 posts 2562 karma points
    Mar 22, 2011 @ 21:56
    Daniel Bardi
    0

    Thumbs Up, Eric!

Please Sign in or register to post replies

Write your reply to:

Draft