<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-5608455.post-113339608080227644</id><published>2005-12-01T01:00:00.000+01:00</published><updated>2005-12-03T14:59:11.306+01:00</updated><title type='text'>Random image rotators everywhere</title><content type='html'>After I came across &lt;a href="http://hivelogic.com/articles/2005/11/30/random_image_rotation_in_rails"&gt;this&lt;/a&gt; I decided that I should write something about the same thing but done in Nevow.&lt;br /&gt;First of all the task is to write a random image rotation implementation for jpegs, gifs and pngs. This is easily accomplished by doing the following 3 things:&lt;br /&gt;&lt;br /&gt;1. Add this to the root Page of your web site. 'random/images/directory' is a directory in your HDD containing lots of images and other random data.&lt;br /&gt;    &lt;pre&gt;&lt;code&gt;child_random_images = static.File('random/images/directory')&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;2. Add this to each of your Page objects that use the random image (or use a common base class to have this behaviour everywhere for free):&lt;br /&gt;    &lt;pre&gt;&lt;code&gt; &lt;br /&gt;def render_random_image(self, ctx, data):&lt;br /&gt;    files = [f for f in os.listdir('random/images/directory') &lt;br /&gt;               if os.path.splitext(f)[1] in ['.jpg', '.gif', '.png']]&lt;br /&gt;    return ctx.tag(src='/random_images/'+random.choice(files))&lt;br /&gt;    &lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;3. Use the following tag wherever you want to have a randomly rotated image in your web app:&lt;br /&gt;    &lt;pre&gt;&lt;code&gt; &amp;lt;img nevow:render="random_image" alt="Random Image" /&amp;gt; &lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;Now you have a wonderfully randomly rotated image in your web browser and with 2 lines less than the Ruby On Rails counterpart without sacrifying readability too much.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5608455-113339608080227644?l=vvolonghi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vvolonghi.blogspot.com/feeds/113339608080227644/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=5608455&amp;postID=113339608080227644' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5608455/posts/default/113339608080227644'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5608455/posts/default/113339608080227644'/><link rel='alternate' type='text/html' href='http://vvolonghi.blogspot.com/2005/12/random-image-rotators-everywhere.html' title='Random image rotators everywhere'/><author><name>Valentino</name><uri>http://www.blogger.com/profile/13519795655711289924</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='08976283372545035870'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>2</thr:total></entry>