Copied to clipboard

Flag this post as spam?

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


  • Donald Swofford 13 posts 107 karma points
    Aug 08, 2014 @ 00:38
    Donald Swofford
    0

    How can I add an if statement?

    I am using the List_Sub_Pages_By_Level macro and was wondering how I can show a headerimage only if there are results?  Below is what I have so far:

    <xsl:if test="SomeExpression">

    <img src="http://myheaderimage.png"/>

          </xsl:if>

    <ul>

    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">

    <li>

    <a href="{umbraco.library:NiceUrl(@id)}">

    <xsl:value-of select="@nodeName"/>

    </a>

    </li>

    </xsl:for-each>

    </ul>

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 08, 2014 @ 08:00
    Dirk De Grave
    3

    you can perform a count() on your for-each expression to check if there are results and show if count() > 0

    <xls:if test="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']) > 0">
    <img src="..." />
    </xsl:if>

    Cheers,

    Dirk

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Aug 08, 2014 @ 08:06
    Dennis Aaen
    2

    Hi Donald.

    When you are saying results, did you mean childs, if so I think that you could something like this.

    <xsl:variable name="count" select="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1'])" />

    <xsl:if test="$count > 0">
        <img src="http://myheaderimage.png"/>
    </xsl:if>

    If you are getting the image from Umbraco by a media picker and want to be possible to set an image each page you can do something like:

    <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/bannerImage, 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>

    Where the bannerImage is the alias of your property, for the media picker. the documentation for the Media Picker http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/built-in-property-editors/Media-Picker and GetMedia library method can be found here: http://our.umbraco.org/wiki/reference/umbracolibrary/getmedia

    And as you probably know there are some different axes in Umbraco parent, children ect. http://our.umbraco.org/wiki/reference/xslt/xpath-axes-and-their-shortcuts

    And here is the documentation of  what Currentpage represent: http://our.umbraco.org/wiki/reference/xslt/understanding-currentpage

     

    Hope this can help you in the right direction.

    /Dennis

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 08, 2014 @ 10:19
    Chriztian Steinmeier
    100

    Hi Donald,

    I'm guessing you want to only show the header image if the for-each expression following it will output something, right?

    In that case, it's very important that you store that result in a variable so you're not repeating it (true for any language, really - not just XSLT) - otherwise, when you (or someone else in a month) need to change the expression, it's very easy to forget to change it in the if statement as well, especially if the header image becomes a header section - you know how it goes :)

    I'd do more than one, actually:

    <xsl:variable name="startNode" select="$currentPage/ancestor-or-self::*[@level = $level]" />
    <xsl:variable name="nodesToShow" select="$startNode/*[@isDoc][not(umbracoNaviHide = 1)]" />
    
    <xsl:if test="$nodesToShow">
        <img src="http://myheaderimage.png" />
    
        <ul>
            <xsl:for-each select="$nodesToShow">
                <li>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                        <xsl:value-of select="@nodeName" />
                    </a>
                </li>
            </xsl:for-each>
        </ul>
    </xsl:if>
    

    I've also put the <ul> inside the if statement because you normally wouldn't want an empty <ul /> in the output anyway. (But you example may have been shortened for this post of course, in which case you should ignore that).

    /Chriztian

  • Donald Swofford 13 posts 107 karma points
    Aug 08, 2014 @ 13:14
    Donald Swofford
    0

    Thanks guys. Dirk I was getting some error so I tried Chriztians solution and it worked so I marked it solved.  I really appreciate the very helpful community here.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 08, 2014 @ 13:31
    Dirk De Grave
    0

    you got it working, that's the important thing!, at least you get to experience a great community ;)

Please Sign in or register to post replies

Write your reply to:

Draft