Copied to clipboard

Flag this post as spam?

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


  • Kate 267 posts 610 karma points
    Jan 19, 2013 @ 16:34
    Kate
    0

    Showing an fallback images on page where no other images is selected

    Hi

    I have a problem in my xslt fil.
    I try to insert a images on a suppage.
    If there is selected an images on the currentpage, the this should be used but if there isen't selected an images, then an images from the level above should be used.

    Does it make any sense?

    This is what I got so fare.

    <xsl:if test="number($currentPage/topBillede) &gt; 0">                          
    <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/topBillede, 0)" />

    <xsl:if test="$media">
    <xsl:variable name="url" select="$media/umbracoFile" />
    <xsl:variable name="width" select="$media/umbracoWidth" />
    <xsl:variable name="height" select="$media/umbracoHeight" />
    <img src="{$url}" width="{$width}" height="{$height}" />
    </xsl:if>
    </xsl:if>

    <xsl:if test="number($currentPage/topBillede) != 1">
    Here I would like the images from the level above
    </xsl:if>

    Hope someone can help :-)

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 19, 2013 @ 17:36
    Tom Fulton
    0

    Hi Kate,

    You can simply use a choose statement to check if the image is selected, and if not, use the image from the parent node.  I extracted your media code into a template to reduce duplication:

     

    <xsl:template match="/">
     <xsl:choose>
       <xsl:when test="number($currentPage/topBillede) &gt; 0">
         <xsl:apply-templates select=$currentPage" mode="image" />
       </xsl:when>
       <xsl:otherwise>
         <xsl:apply-templates select=$currentPage//parent::* [@isDoc]" mode="image" />
       </xsl:otherwise>
     </xsl:choose>
    </xsl:template>

    <xsl:template match="* [@isDoc]" mode="image">
     <xsl:variable name="media" select="umbraco.library:GetMedia(topBillede, false())" />
     <xsl:if test="$media">
       <xsl:variable name="url" select="$media/umbracoFile" />
       <xsl:variable name="width" select="$media/umbracoWidth" />
       <xsl:variable name="height" select="$media/umbracoHeight" />
       <img src="{$url}" width="{$width}" height="{$height}" />
      </xsl:if>
    </xsl:template>

    Hope this helps,
    Tom 

  • Kate 267 posts 610 karma points
    Jan 20, 2013 @ 10:02
    Kate
    0

    Hi Tom

    I think Im almost there now.

    So fare I got this

    <xsl:template match="/">
    <xsl:choose>
    <xsl:when test="number($currentPage/topBilledeSubPages) &gt; 0">
    <xsl:apply-templates select="$currentPage" mode="image" />
    </xsl:when>
    <xsl:otherwise>
    <xsl:apply-templates select="$currentPage/parent::*[@isDoc]" mode="image" />
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>

    <xsl:template match="* [@isDoc]" mode="image">
    <xsl:variable name="media" select="umbraco.library:GetMedia(topBilledeSubPages, false())" />
    <xsl:if test="$media">
    <xsl:variable name="url" select="$media/umbracoFile" />
    <xsl:variable name="width" select="$media/umbracoWidth" />
    <xsl:variable name="height" select="$media/umbracoHeight" />
    <img src="{$url}" width="{$width}" height="{$height}" />
    </xsl:if>
    </xsl:template>

    I know I said that I wanted to get the images from the level above. But I just realised that if level1 has a images and level2 and level3 does not have an images I what them both to get the images from level1.

    The way it works now is that it only gets the images from the level above = level2 gets the images from level1, level3 gets this Error parsing XSLT file: \xslt\TopSlider.xslt

    Hope you still have time to help

    Kate

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 20, 2013 @ 20:33
    Tom Fulton
    0

    Hi Kate,

    Ah, that makes sense.  This code assumes the parent will always have an image.  I think what you're looking for is to recursively look for an image - that is, check each parent until we find an image.  

    To get a property recursively in XSLT, you can use:

    <xsl:apply-templates select="ancestor-or-self::*[normalize-space(topBilledeSubPages)][1]/topBilledeSubPages" />

    Here's what I might do in your code:

    <xsl:template match="/">
     <xsl:apply-templates select="$currentPage/ancestor-or-self::*[normalize-space(topBilledeSubPages)][1]/topBilledeSubPages" />
     <!-- This will find the first parent (or current page) where topBilledeSubpages has a value, then "pass it in" to the template below -->
    </xsl:template>

    <xsl:template match="topBilledeSubPages">
     <xsl:variable name="media" select="umbraco.library:GetMedia(., false())" />
     <xsl:if test="$media">
       <xsl:variable name="url" select="$media/umbracoFile" />
       <xsl:variable name="width" select="$media/umbracoWidth" />
       <xsl:variable name="height" select="$media/umbracoHeight" />
       <img src="{$url}" width="{$width}" height="{$height}" />
     </xsl:if>
    </xsl:template>

    Hope this helps,
    Tom

  • Kate 267 posts 610 karma points
    Jan 21, 2013 @ 09:30
    Kate
    0

    It works perfectly.

    Thanks for your help :-)

Please Sign in or register to post replies

Write your reply to:

Draft