Random quote using macro? Options
tkahn
Posted: Thursday, November 29, 2007 4:43:03 PM

Rank: Fanatic

Joined: 11/24/2006
Posts: 297
Location: Stockholm, Sweden
I'm looking for idéas on how to create a macro that picks a random quote every time the user reloads the page.

Judging from previous posts here on the forum I understand that randomizing does not exist in XSLT. Therefore I guess I'd have to create some sort of hybrid between XSLT and C#? Does anyone have a working (3.0) macro that does randomizing that I could look at? If not, do you have any general pointers how I should solve this problem?

Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
SoerenS
Posted: Thursday, November 29, 2007 5:21:57 PM

Rank: Fanatic

Joined: 7/25/2006
Posts: 366
Location: Silkeborg, Denmark
Have you tried searching for "random" on these forums :-P

There's also this old example on how to find a random node:
http://en.wikibooks.org/wiki/Umbraco/Samples_and_Articles/XSLT/Extend_Xsl

/SoerenS

Brug for råd til hvordan du driver en god webshop? / Need advice on how to run an effective webshop?
PeterD
Posted: Thursday, November 29, 2007 5:23:27 PM

Rank: Fanatic

Joined: 7/20/2006
Posts: 409
Location: NL
I do have a site running which shows a random picture/flash-file on each reqeusted page.
All done in a macro, site is running v3.02

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:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:math="urn:schemas-hizi-nl:math"
    xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings"
    xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath"
    exclude-result-prefixes="msxml Exslt.ExsltMath Exslt.ExsltStrings math umbraco.library">


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

<xsl:param name="currentPage"/>

<msxml:script language="JavaScript" implements-prefix="math">
function random(numDie,numMax,numMin){
if (numMin==null){numMin=1;}
var sum=0;
for (var index=0;index&lt;numDie;index++){
sum+=Math.floor(Math.random()*(numMax-numMin) + numMin);
}
return "" + sum;
}
function floorme(numFloor){
return "" + Math.floor(numFloor);
}
</msxml:script>

<xsl:variable name="item" select="/macro/linksource"/>
<xsl:variable name="parent" select="umbraco.library:GetMedia(1063, true())" />
<xsl:variable name="random" select="math:random(1,count($parent/node)+1,1)"/>
<xsl:template match="/">

<xsl:for-each select="$parent/node">
<xsl:if test="position()=$random">
<xsl:if test="./data [@alias = 'umbracoExtension'] = 'swf'">
<script type="text/javascript">
var FO = { movie:"<xsl:value-of select="./data [@alias = 'umbracoFile']"/>", width:"160", height:"230", majorversion:"7", build:"0", menu:"false", quality:"best", wmode:"transparent", bgcolor:"#FFFFFF", allowscriptaccess:"samedomain" };
UFO.create(FO, "righttop");
</script>
<div id="righttop">
<p>Hier staat reclame</p>
</div>
</xsl:if>
<xsl:if test="./data [@alias = 'umbracoExtension'] = 'gif'">
<img alt="">
<xsl:attribute name="src"><xsl:value-of select="./data [@alias = 'umbracoFile']"/></xsl:attribute>
</img>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


Hope this is of some help

PeterD

FOR EACH brokenitem EXCLUSIVE-LOCK:
RUN FixBrokenItem.
RUN ImproveBrokenItem.
END.
timgaunt
Posted: Wednesday, February 06, 2008 1:27:10 PM

Rank: Aficionado

Joined: 10/31/2007
Posts: 109
Location: Birmingham (UK)
Hi Peter,

I'm trying to do something similar by selecting a random header image from a media folder however whenever I try this code I get "System.Xml.XmlException: The ';' character, hexadecimal value 0x3B, cannot be included in a name. Line 22, position 30."

The offending line appears to be:
for (var index=0;index<numDie;index++){

"numDie;" to be specific.

Any ideas why this might be?

Tim

Managing Director at The Site Doctor Ltd - My personal blog is here - Umbraco Newbie ;)
PeterD
Posted: Wednesday, February 06, 2008 2:00:09 PM

Rank: Fanatic

Joined: 7/20/2006
Posts: 409
Location: NL
Yes, the line should be:

for (var index=0;index&lt;numDie;index++){


FOR EACH brokenitem EXCLUSIVE-LOCK:
RUN FixBrokenItem.
RUN ImproveBrokenItem.
END.
timgaunt
Posted: Wednesday, February 06, 2008 2:24:53 PM

Rank: Aficionado

Joined: 10/31/2007
Posts: 109
Location: Birmingham (UK)
Of course, thanks :)

Tim

Managing Director at The Site Doctor Ltd - My personal blog is here - Umbraco Newbie ;)
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.