Follow Sebastiaan de Jonge on Twitter
Sebastiaan de Jonge

Blog

25Jan

Cool TypoScript: Display random content elements

Posted on January 25, 2010 in TypoScript by Sebastiaan de Jonge

If 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 
  1. lib.quotes = COA_INT
  2. lib.quotes {
  3.     10 = CONTENT
  4.     10{
  5.         table=tt_content
  6.         select{
  7.             pidInList = 91
  8.             max = 1
  9.             orderBy = RAND()
  10.         }
  11.         renderObj = TEXT
  12.         renderObj.field = header
  13.     }
  14. }
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.

Comments (0)

    Got something to say?

     
    Notify me when someone adds another comment to this post
     

    Search

    Categories

    Tags

    Archive

    Blogroll

    Syndicate