Cool TypoScript: Display random content elements
Posted on January 25th 2010 at 18:52 inside TypoScript with 0 commentsIf you're not aware of the capabilities of TypoScript, you would start looking in the repository for something usable. I immedeatly remember using something simular for getting a content element from a different page, and displaying it. Combining this with a COA_INT, to prevent caching of the element, I was able to do this in under 5 minutes with only 15 lines of TypoScript code (including marker assignment).
The Code
Display random content elements 
lib.quotes = COA_INT
lib.quotes {
10 = CONTENT
10{
table=tt_content
select{
pidInList = 91
max = 1
orderBy = RAND()
}
renderObj = TEXT
renderObj.field = header
}
}
It works rather simple:
- We create a COA_INT to prevent the element from being cached
- We select 1 element from the tt_content table, from our storage page (SysFolder, 91)
- We order our selection by RAND() (MySQL) in order to make the content random.
- We just add the header to be rendered and displayed.
That's it! The downside is that RAND() doesn't work in MSSQL as it works in MySQL. For this we can use NEWID() instead of RAND(). I'm pretty sure something simular is also possible for other database systems, haven't looked it up or tested it though.




Your comment