Displaying a Random Quote

by paulbhartzog — last modified Dec 30, 2008 03:01 PM
How to display a random quote on a Plone page.

A lot of people keep asking me about the random quote generator on http://www.panarchy.com so here it is:

  1. put this python script named "getQuote" in a folder named "quotes" in the root of your site:
    import string
    from random import choice
    all_quotes = string.split(context.quotes.quotes_file.data,"\n")
    all_quotes.pop()
    quote = choice(all_quotes)
    fields = string.split(quote,"     ")
    item_template = """<div style="text-align:left;margin: 15px;">
       <span class="quote">"%s"</span>
       <p class="cite">—%s</p>
       </div>
       """
    print item_template % (fields[1],fields[0])
    return printed
    
  2. in the same place put a File named "quotes_file" that looks like this:
    John Perry Barlow     Chaos becomes opportunity.
    Author     quote
    
    NOTE: I'm using 5 spaces as a separator; you can use anything you want.
  3. call it from main_template like this:
    <div class="randquote" tal:content="structure here/quotes/getQuote">
    </div>
    
  4. tweak your CSS; mine looks like this:
    .randquote {
       border: solid 1px &dtml-globalBorderColor;;
       background-color: &dtml-globalBackgroundColor;;
       padding: 0 1em;
       margin: 2em 2em 0em 2em;
    }
    .quote {
       font-family: Verdana, sans-serif;
    }
    .cite {
       text-align: right;
       margin-bottom: 0px;
       font-family: Verdana, sans-serif;
    }
    

that's it.

Thanks to Jeremy, and the folks on the Plone list for help.

-Paul