Copied to clipboard

Flag this post as spam?

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


  • Sean Parsons 23 posts 38 karma points
    Sep 20, 2009 @ 10:48
    Sean Parsons
    0

    Passing variables into XSLT macros

    Hi all,

     

    I know this is a very basic question, and in fact this code is based on another topic that I saw earlier. But for the life of me, I can't get this to work.

    Please could someone check this code to see if there's a glaring error in it that I've been looking at for too long...

     

    <?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:template match="/">
    <xsl:variable name="imgAlias" select="/macro/imgAlias"/>
    
    <!-- start writing XSLT -->
    
    <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>
            <!--<xsl:attribute name="width">
                <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias=$imgAlias], 'false')/data [@alias='umbracoWidth']" />
            </xsl:attribute>
            <xsl:attribute name="height">
                <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias=$imgAlias], 'false')/data [@alias='umbracoHeight']" />
            </xsl:attribute>-->
            <xsl:attribute name="alt">
                <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias=$imgAlias], 'false')/@nodeName" />
            </xsl:attribute>
        </img>
    
    </xsl:if>
    
    </xsl:template>
    
    </xsl:stylesheet>

    The test 

    <xsl:if test="$imgAlias != ''">

    always is empty... (I've also tried using <xsl:param name="imgAlias" select="/macro/imgAlias"/> to no avail)

    The calling code is

    <umbraco:Macro Alias="CMCCreateImageTag" imgAlias="CMCHomePageAboutImage" runat="server"></umbraco:Macro>
    
    Thanks for the help - sure it's something simple.
  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 20, 2009 @ 13:03
    Thomas Höhler
    0

    Put the param for the macro outside of the 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"
           
    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="imgAlias" select="/macro/imgAlias"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->

    <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>
               
    <!--<xsl:attribute name="width">
                    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias=$imgAlias], 'false')/data [@alias='umbracoWidth']" />
                </xsl:attribute>
                <xsl:attribute name="height">
                    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias=$imgAlias], 'false')/data [@alias='umbracoHeight']" />
                </xsl:attribute>-->

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

    </xsl:if>

    </xsl:template>

    </xsl:stylesheet>

    And check for typos. Do  you have the right aliases etc.

    hth, Thomas

  • Sean Parsons 23 posts 38 karma points
    Sep 20, 2009 @ 13:14
    Sean Parsons
    0

    Hi Thomas, and thanks for the reply.

     

    I have moved the param to where you indicated, with no luck...

    ...
    ...
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    
    <xsl:param name="currentPage"/>
    <xsl:param name="imgAlias" select="/macro/imgAlias"/>
    
    <xsl:template match="/">
    
    <!-- start writing XSLT -->
    
    <xsl:if test="$imgAlias != ''">
    ... etc...

    If I hardcode the imgAlias name I get the image displayed

    ...
    ...
    <xsl:param name="currentPage"/>
    <xsl:param name="imgAlias" select="'CMCHomePageAboutImage'"/>
    ... etc...

    So I'm at a loss as to why the parameter is not passed into the macro.

    Is there a way to output the macro XML so that I can check if it's being passed in at all?

    Thanks again,

    Sean

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 20, 2009 @ 18:09
    Douglas Robar
    0

    Be sure that you have the parameter set up in the MACRO as well, and watch that you've got the case of the alias the same. So...

    Go to the Developer section of umbraco and expand the MACRO tree. Select your CMCCreateImageTag macro. Click on the PARAMETERS tag. Check that you've got a macro parameter with an alias of imgAlias and a type of text (I assume you're passing in a string but if not select the proper data type for the macro parameter). Save the macro.

    That should do it.

    For debugging, you could add the following at the top of your template to display all that is sent in to your xslt...

    <xsl:template match="/">
        <textarea>
            <xsl:copy-of select="/macro" />
        </textarea>
        <!-- the rest of your template goes here -->

     

    One further note... it doesn't matter if you get the xsl:param before the <template match="/"> or inside it. The difference is simply one of scoping... it is a global parameter if outside the template and local to the template if inside. In your example it wouldn't matter since you've only got one <template> anyway.

    Let us know what you find out.

    cheers,
    doug.

  • Sean Parsons 23 posts 38 karma points
    Sep 20, 2009 @ 18:25
    Sean Parsons
    0

    As simple as that! That's perfect, Doug, thanks a million. 

    I was attempting to view the /macro xml but obviously nothing was coming through as I had no parameters set up. Somehow I thought that was done behind the scenes, but looking at the parameters now I see it's more powerful than I though.

    Cheers,
    Sean

  • Aleksander 26 posts 56 karma points
    Feb 18, 2011 @ 14:13
    Aleksander
    0

    Hello Doug!

    I have this pieces of code in my xslt:

    <xsl:param name="dClass" select="/macro/cssClass"/>

    <xsl:value-of select="concat('&lt;div class=&quot;', $dClass,'&quot;', ' style=&quot;background-image:url(', $pic, ');background-size: cover;
    -moz-background-size: cover;-webkit-background-size: cover;-o-background-size: cover;&quot;&gt;')" disable-output-escaping="yes"/>

    There is a parameter with aliase "cssClass" in my macro. But the control is visualised like this:

    <div class="" style="background-image:url(/media/5606/ygradient.jpg);background-size: cover;
    -moz-background-size: cover;-webkit-background-size: cover;-o-background-size: cover;"
    >

    The class="" - there is no parameter value passed. I add this to debug:

    <textarea>
        <xsl:value-of select="$dClass"/>
     </textarea>


    and there is really no value. Can you help with this?

    Regards,

    Aleksandar.

     

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 18, 2011 @ 15:27
    Kim Andersen
    0

    Hi Aleksander

    Could you show us the code from where you insert the macro in your template?

    /Kim A

  • Aleksander 26 posts 56 karma points
    Feb 18, 2011 @ 15:34
    Aleksander
    0

    Hello!

    Here is the code:

    <?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="dClass" select="/macro/cssClass"/>
      
    <xsl:template match="/">
      <textarea>
        <xsl:value-of select="$dClass"/>
        </textarea>
      <xsl:choose>
          <xsl:when test="$currentPage/mainBackground = '' ">
             <xsl:variable name="pic">
               <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::* [@isDoc]/mainBackground, 0)/umbracoFile"/>
             </xsl:variable>
             <xsl:value-of select="concat('&lt;div class=&quot;', $dClass,'&quot;', ' style=&quot;background-image:url(', $pic, ');background-size: cover;
    -moz-background-size: cover;-webkit-background-size: cover;-o-background-size: cover;&quot;&gt;')" disable-output-escaping="yes"/>
          </xsl:when>
          <xsl:otherwise>
             <xsl:variable name="pic">
               <xsl:value-of select="umbraco.library:GetMedia($currentPage/mainBackground, 0)/umbracoFile"/>
             </xsl:variable>
             <xsl:value-of select="concat('&lt;div class=&quot;', $dClass,'&quot;', ' style=&quot;background-image:url(', $pic, ');background-size: cover;
    -moz-background-size: cover;-webkit-background-size: cover;-o-background-size: cover;&quot;&gt;')" disable-output-escaping="yes"/>
          </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

    </xsl:stylesheet>

     

    Regards,

    Aleksandar

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 18, 2011 @ 15:39
    Kim Andersen
    0

    Hi Aleksander.

    I was thinking of the code where you are inserting the macro. The code from your template that looks something like this:

    <umbraco:Macro ... ... >

    /Kim A

  • Aleksander 26 posts 56 karma points
    Feb 18, 2011 @ 15:43
    Aleksander
    0

    Ooo, sorry! Here it is:

    <umbraco:Macro cssClass="mainBlock" Alias="Background" runat="server"></umbraco:Macro>
  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 18, 2011 @ 15:45
    Kim Andersen
    0

    Hmm...okay.

    Could you try removing the first slash from the dClass variable like this:

    <xsl:variable name="dClass" select="macro/cssClass" />

    /Kim A

  • Aleksander 26 posts 56 karma points
    Feb 18, 2011 @ 15:46
    Aleksander
    0

    Already did - nothing changes.

    Aleksander

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 18, 2011 @ 15:49
    Kim Andersen
    0

    And you have declared the parameter on the macro inside Umbraco right? On the macro in the developer section.

    /Kim A

  • Aleksander 26 posts 56 karma points
    Feb 18, 2011 @ 15:51
    Aleksander
    0

    Yes, it was a working macro. I just raplaced some hard-cored text with the varriable mentioned above.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 18, 2011 @ 20:50
    Kim Andersen
    0

    Okay, that is very weird. Could you maybe try with another macro parameter? Just a test param, that you'll delete after testing.

    /Kim A

  • Nathan Scherff 2 posts 22 karma points
    Mar 20, 2011 @ 12:44
    Nathan Scherff
    0

    Aleksander.
    Ik had the same issue. It looked like it had something to do with the name cssClass. I changed it to something else and then it worked.

  • Aleksander 26 posts 56 karma points
    Mar 20, 2011 @ 17:08
    Aleksander
    0

    Hello!

    Yes, thanks to Kim, I already decide the problem by changing the name.

    Thanks,

    Aleksandar

Please Sign in or register to post replies

Write your reply to:

Draft