Copied to clipboard

Flag this post as spam?

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


  • Sajid Riaz 142 posts 165 karma points
    Jul 13, 2010 @ 22:41
    Sajid Riaz
    0

    call template

    Howdy pardners,

     

    <xsl:call-template name="buildArray">
    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/node/data[@alias='umbracoFile']"/>
    </xsl:call-template>

    I'm using the xslt above.  From what little i know of xslt the above is retrieving the umbracoFile value and passing to buildArray correct so far?

    here's the build array part:

    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
        <xsl:for-each select="$arrayNodes">
            <xsl:value-of select="myFuncs:addtoArray(string(./text()))"/>
        </xsl:for-each>
    </xsl:template>

    What I want to do is not only pass the umbracofile but also pass a caption (a property i added to the image datatype)

    can some clever dude give me a clue as how to do this.

    thanks everyone

    >sajid

     

     

     

  • Tom Fulton 2030 posts 4996 karma points c-trib
    Jul 13, 2010 @ 22:50
    Tom Fulton
    0

    Not too sure what you're doing with the addtoArray stuff, but just to pass another parameter into your template is simple:

     

    First add it to your call-template statement:

    <xsl:call-template name="buildArray">
    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/node/data[@alias='umbracoFile']"/>
    <xsl:with-param name="imgCaption" select="umbraco.library:GetMedia($imageRoot, 'true')/node/data[@alias='yourCaptionFieldName']"/>
    </xsl:call-template>

    Then add it to the buildArray template

    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
    <xsl:param name="imgCaption"/>
    <xsl:for-each select="$arrayNodes">
       <xsl:value-of select="myFuncs:addtoArray(string(./text()))"/>
        Caption: <xsl:value-of select="$imgCaption"/>
    </xsl:for-each>
    </xsl:template>

     

     

     

     

  • Sajid Riaz 142 posts 165 karma points
    Jul 13, 2010 @ 23:14
    Sajid Riaz
    0

    Hey Tom,

    Thanks for the quick response buddy. great response too, but still not working. 

    yr xslt above nearly works.  it brings back a random image but the caption does not change.

    you see i uploaded images to my media section and gave them a caption too.  So what i want is to bring back a random image and its associated caption.

    here's the complete xslt if it helps:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>

    <xsl:stylesheet
        version="1.0"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        xmlns:myFuncs="urn:my-scripts"
        exclude-result-prefixes="msxml myFuncs umbraco.library">


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

    <xsl:param name="currentPage"/>

    <xsl:variable name="imageRoot"     select="$currentPage/ancestor-or-self:: node[@level = 1]/data [@alias = 'frontPageImages']"/>

    <msxsl:script implements-prefix="myFuncs" language="JavaScript">
    <![CDATA[

    var myArray = new Array();

    function random(){    
        var index = Math.floor(Math.random() * myArray.length);   
        return myArray [index];
    }

    function addtoArray(item){
        myArray.push(item);
    }

    ]]>

    </msxsl:script>

    <xsl:template match="/">


    <xsl:call-template name="buildArray">
    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/node/data[@alias='umbracoFile']"/>
    <xsl:with-param name="imgCaption" select="umbraco.library:GetMedia($imageRoot, 'true')/node/data[@alias='caption']"/>
    </xsl:call-template>



    <img>
    <xsl:attribute name="src">
    <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
       <xsl:value-of select="myFuncs:random()"/>
    <xsl:text>&amp;width=400</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:text>width:400px;</xsl:text>
    </xsl:attribute>
    </img>
    <p id="caption">
    <xsl:value-of select="$currentPage/data[@alias='caption']"/>
    </p>


    </xsl:template>

    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
        <xsl:for-each select="$arrayNodes">
            <xsl:value-of select="myFuncs:addtoArray(string(./text()))"/>
        </xsl:for-each>

    </xsl:template>
    </xsl:stylesheet>

     

  • wolulcmit 357 posts 693 karma points
    Jul 13, 2010 @ 23:20
    wolulcmit
    0

    have you tried
    <xsl:value-of select="umbraco.library:GetMedia($imageRoot, 'true')/node/data[@alias='caption']"/>
    instead of
    <xsl:value-of select="$currentPage/data[@alias='caption']"/>

  • Sascha Wolter 615 posts 1101 karma points
    Jul 14, 2010 @ 01:01
    Sascha Wolter
    0

    Hi Sajid,

    out of the top of my head I would do something like this:

    Javascript

    var myArray = new Array();

    var randomValue;

     

    //generate random number

    function generateRandom(){     

        randomValue = Math.floor(Math.random() * myArray.length);    

    }

     

    //get image url from array

    function getUrl(){

    return myArray[randomValue].url;

    }

     

    //get caption from array

    function getCaption(){

    return myArray[randomValue].caption;

    }

     

    function addtoArray(itemUrl, itemCaption){

        var item = {url: itemUrl, caption: itemCaption};

        myArray.push(item);

    }

     

    Xslt:

    <xsl:call-template name="buildArray">
    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/node"/>
    </xsl:call-template>


    <xsl:value-of select="myFuncs:generateRandom()" />
    <img>
    <xsl:attribute name="src">
    <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
       <xsl:value-of select="myFuncs:getUrl()"/>
    <xsl:text>&amp;width=400</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:text>width:400px;</xsl:text>
    </xsl:attribute>
    </img>
    <p id="caption">
    <xsl:value-of select="myFuncs:getCaption()"/>
    </p>


    </xsl:template>

    //pass in the whole media nodes which contain the url and caption, instead of just passing in the urls and caption texts separately

    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
        <xsl:for-each select="$arrayNodes">

            <xsl:value-of select="myFuncs:addtoArray(current()/data[@alias='umbracoFile'], current()/data[@alias='caption'])"/>

        </xsl:for-each>

    </xsl:template>
    </xsl:stylesheet>

     

    Hope that all makes sense,

    Sascha

  • Sajid Riaz 142 posts 165 karma points
    Jul 14, 2010 @ 11:40
    Sajid Riaz
    1

    Many thanks dudes...you guys are terrific.

    Sascha your code is Excellent man!!! thanx.

    Javascript works perfectly in a normal  web page with no xslt so i know its correct. i mean it gets random image and its corresponding caption on page refresh.

    however when used inconjunction with the xslt it doesn't seem to refresh or regenerate a random number.  The image and caption remain as per first page request.

    I'm still investigating this but 98% there. thanks to you.

     

    >sajid

  • Sajid Riaz 142 posts 165 karma points
    Jul 14, 2010 @ 20:37
    Sajid Riaz
    0

    Thanx Sascha...it worked.

     

    changed: <xsl:value-of select="myFuncs:addtoArray(current()/data[@alias='umbracoFile'], current()/data[@alias='caption'])"/>

    to:  <xsl:value-of select="myFuncs:addtoArray(string(./data[@alias='umbracoFile']),string(./data[@alias='caption']))"/>


  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies