Copied to clipboard

Flag this post as spam?

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


  • Sam 184 posts 209 karma points
    Sep 14, 2010 @ 19:57
    Sam
    0

    If field doesn't exist , use parent field

    Hi everyone,

    So far I have this code here (using umbraco 4.0.4.2)

    <xsl:if test="$imgAlias != ''">
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias=$imgAlias], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
    </img>
    </xsl:if>

    I have pages which display a header image when there is an image in the field (imgAlias) which I choose via media picker. However, one of the pages has sub pages, on these I just want to use the same header image as the parent page.

    ie test if image is present, if so, use that, if not (I mean the field imgAlias not at all in the document type, not just no image picked - so Im guessing that's 'null' rather than 'empty'), then use parent page image

    I have seen that you can you choose, and otherwise xsl commands, however, my slow learning curve continues and I am stuck on what appears to be something quite simple :( please help if anyone can, thanks :)

    Sam.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 14, 2010 @ 21:42
    Dirk De Grave
    0

    Perform a check first whether there's a property with alias = 'imgAlias' and whether that property has a value using the single statement

    <xsl:if test="$currentPage/data [@alias = 'imgAlias'] != ''">

    If value is not an empty string, than just use the code as you've specified already, otherwise get parent node of $currentPage using 

    $currentPage/parent::node

     

    "ie test if image is present, if so, use that, if not (I mean the field imgAlias not at all in the document type, not just no image picked - so Im guessing that's 'null' rather than 'empty'), then use parent page image"

    --> no need for a check on null, it's implicitely done by the first statement

     

    Hope this helps.

    Regards,

    /Dirk

  • Sam 184 posts 209 karma points
    Sep 14, 2010 @ 22:54
    Sam
    0

    Sorry Dirk, could you just help me out with one more thing? Could I also clarify that this maco runs in the master template and appears in the header on ALL pages. So I cant make two macros, and two templates, and put one macro in each of the two (which would work but not what I want to achieve).

    This code displays the correct header (worked on the upper level pages): (xlst error on sub pages)

    <xsl:if test="$imgAlias != ''">
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias=$imgAlias], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
    </img>
    </xsl:if>

     

    the folowing displays the correct parent header (worked on the sub pages): (xlst error on upper level pages)

    <xsl:if test="$imgAlias != ''">
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/data [@alias=$imgAlias], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
    </img>
    </xsl:if>

     

    How do I join these to together into one piece of code? I want it to state if theres a header image, display it, otherwise display the parent image (as it does in second bit of code).

    Thanks,

    Sam.

  • Sam 184 posts 209 karma points
    Sep 14, 2010 @ 23:12
    Sam
    0

    Tried this but no joy :(

    <xsl:choose>

    <xsl:when test="$currentPage/data [@alias = 'imgAlias'] != ''">
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias=$imgAlias], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
    </img>
    </xsl:when>

    <xsl:otherwise>

    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia(parent::node/data [@alias=$imgAlias], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
    </img>

    </xsl:otherwise>

    </xsl:choose>

    Can't see what I'm doing wrong, want to display a parent image (with alias imgAlias) if the current page has no alias (not if it's empty, just if it's not there at all in the doctype). Anyone see where I'm going wrong? Thanks if you can help :)

    Sam.

  • Sam 184 posts 209 karma points
    Sep 15, 2010 @ 22:20
    Sam
    0

    Hmmmmmm, seemed to be getting a bit closer but getting currentpage not defined or out of scope now...

    <xsl:choose>

    <xsl:when test="$currentPage/data [@alias = 'HeaderBgImage'] != ''">
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias = 'HeaderBgImage'], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
    </img>
    </xsl:when>

    <xsl:otherwise>

    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentpage/parent::node/data [@alias = 'HeaderBgImage'], 'false')/data [@alias='umbracoFile']" />
      </xsl:attribute>
    </img>
    </xsl:otherwise>

    </xsl:choose>


    Can anyone help?? Thanks,

    Sam.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 15, 2010 @ 22:25
    Dirk De Grave
    0

    the variable $currentPage should never "get" out of scope, so how did you define this var in your xslt. Here's an example of how it should be included in your xslt (I've just created a new xslt (clean template):

     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:PDF="urn:PDF" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets PDF ">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <!-- start writing XSLT -->
    </xsl:template>
    </xsl:stylesheet>
    

    Hope this helps.

    Regards,

    /Dirk

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • Sam 184 posts 209 karma points
    Sep 16, 2010 @ 09:10
    Sam
    0

    Thanks for your patience Dirk, this is my first xlst, but finally, success! I had forgot to add the alias into the parameter tab in the macro and was using an incorrect alias for the image... doh! This is the whole thing in case any else has the same situation :)

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">


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

    <xsl:param name="currentPage"/>
    <xsl:param name="HeaderBgImage" select="/macro/imgAlias"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->

    <xsl:choose>

    <xsl:when test="$currentPage/data [@alias = 'HeaderBgImage'] != ''">
    <img>
    <xsl:attribute name="src">
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias = 'HeaderBgImage'], 'false')/data [@alias='umbracoFile']" />
    </xsl:attribute>
    </img>
    </xsl:when>

    <xsl:otherwise>
    <img>
    <xsl:attribute name="src">
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/data [@alias = 'HeaderBgImage'], 'false')/data [@alias='umbracoFile']" />
    </xsl:attribute>
    </img>
    </xsl:otherwise>

    </xsl:choose>



    </xsl:template>

    </xsl:stylesheet>

    Thanks again Dirk :)

    Best regards,

    Sam.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 16, 2010 @ 09:21
    Dirk De Grave
    0

    Glad I could help, it requires some trial and error sometimes, but once you get the grips... Anyway, good luck on your journey.

     

    Cheers,

    /Dirk

  • Sam 184 posts 209 karma points
    Sep 16, 2010 @ 09:26
    Sam
    0

    Thanks, all the best.

    Sam.

  • Rik Helsen 670 posts 873 karma points
    Sep 16, 2010 @ 09:31
    Rik Helsen
    0

    Here is a variation on your script, that walks all the way up to content tree untill it finds an image defined for the header:

                  <img>
    <xsl:attribute name="src">
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/ancestor-or-self::node [data[@alias='headerimage']!=''][1] /data[@alias='headerimage'],'false')/data [@alias = 'umbracoFile']"/>
    </xsl:attribute>
    </img>

    And here is the same with an added check if any exists at all and a fallback image

              <xsl:choose>
    <xsl:when test="$currentPage/ancestor-or-self::node [string(data[@alias='headerimage'])!=''][1] /data[@alias='headerimage'] != ''">
    <img>
    <xsl:attribute name="src">
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/ancestor-or-self::node [data[@alias='headerimage']!=''][1] /data[@alias='headerimage'],'false')/data [@alias = 'umbracoFile']"/>
    </xsl:attribute>
    </img>
    </xsl:when>
    <xsl:otherwise>
    <img src="/img/header_partikulier.jpg" alt="" />
    </xsl:otherwise>
    </xsl:choose>
  • Sam 184 posts 209 karma points
    Sep 16, 2010 @ 11:03
    Sam
    0

    Thanks Rik,

    Totally works a charm :)

    Sam.

Please Sign in or register to post replies

Write your reply to:

Draft