MTRandomLine

Current version is 1.1 (23 Feb 2003)

This plugin gives you a new Movable Type template tag: <$MTRandomLine$>. This tag will extract and display one or more random lines from either a text file, or a Movable Type template module.

For example, you could create a list of favourite quotes, and use this tag to display a random selection from this list on your home page.

The random selection will only refresh when you rebuild the template containing the tag. It will not refresh whenever someone visits the page. If you want to do that, you will have to employ some kind of server-side or client-side scripting solution.

  1. Download the latest version of the plugin
  2. Unzip the contents of the file
  3. Copy the file RandomLine.pl to your Movable Type "plugins" directory. (If you don't already have a plugins directory, create it in the same directory where your mt.cgi file is located, and then upload RandomLine.pl)
  4. Change the permissions on RandomLine.pl to 755

The tag has six parameters: file, module, allowblanks, num, glue, and allowduplicates.

file

This tells the tag what file it should take a random line from. Note that this is the full file path to the file--it isn't relative to your blog directory. On unix systems, it will look something like "/home/username/www/quotes.txt". On Windows systems, it will look something like "E:\inetpub\wwwroot\username\quotes.txt"

Example:

<$MTRandomLine file="/home/username/www/quotes.txt"$>

module

This tells the tag what template module it should take a random line from. This is just the name of a template module in your movable type system.

Example:

<$MTRandomLine module="quotes"$>

(Note that if you specify both a file and a module, only the module will be used.)

allowblanks

By default, <MTRandomLine> will skip blank lines in your input file. If you want blank lines to be included as possible output, set this parameter to "1".

Example:

<$MTRandomLine module="quotes" allowblanks="1"$>

num

This tells the tag how many lines should be returned from the file or module. The default value is "1", so if you only want to show one random line, you don't have to specify this parameter at all. If you want to show multiple random lines, put the relevant number here.

Example:

<$MTRandomLine module="quotes" num="3" $>

glue

This tells the tag how multiple random lines should be joined together. It is a string of text (or HTML) that will be inserted between all random lines returned by the tag. Note that it does not appear before the first returned value, or after the last one. The example below shows how you can make line breaks appear between a set of three random quotes.

Important note: if you want to include any HTML in the "glue" parameter, you must make sure that all < and > characters (such as in a line break, <br />) are written as HTML entities, i.e. &lt; and &gt;. If you don't do this, Movable Type may interpret them as part of the tags in your template, instead of as part of the glue. Strange results (incorrect page rendering, human sacrifice, cats and dogs living together, mass hysteria, etc.) will follow.

Example:

<$MTRandomLine module="quotes" num="3" glue="&lt;br /&gt;" $>

allowduplicates

In most circumstances, you will want the values returned by the RandomLine plugin to be unique. If you want to display three random quotes, for example, you probably don't want the tag to return the same quote three times (which can happen, if you're selecting purely at random). So, by default, the tag makes sure that duplicate values cannot be returned. If you want to change this behaviour, you can set the allowduplicates parameter to "1", which will allow duplicates to come through.

Example:

<$MTRandomLine module="alphabetletters" num="40" glue="-" allowduplicates="1" $>

1. Random quote

Create a new template module in your Movable Type blog. Fill it with quotes, making sure to put each quote on a separate line. (Each line is considered a separate entity. The examples here may look like they span multiple lines, but that's only because the browser wraps lines.)

Template module: "quotes"
If you ignore it for long enough, it will go away.
A vote for democracy is a vote for mob rule.
If you have to look up the help file to get started, it's too complicated

Then, put the <$MTRandomLine$> tag in the index or archive template where you want the quote to appear:

Index template
...
<$MTBlogName$>
<blockquote>"<$MTRandomLine module="quotes"$>"</blockquote>
...

Note that in this example, the quotation marks ("") are placed outside of the <$MTRandomLine$> tag. This means that you don't have to worry about opening and closing quotation marks whenever you add a new line to your quotes file. If you want your quotes to take on fancier formatting, or if you want to vary the formatting based on the quote, you can embed your HTML in the quotes file, e.g.:

Template module: "formattedquotes"
<div class="cynicalquote">"If you ignore it for long enough,<br /> it will go away."</div>
<div class="cynicalquote" style="color:red;font-weight:bold">"A vote for democracy is a vote for mob rule."</div>
<div class="usabilityquote">If you have to look up the help file to get started, it's too complicated</div>
Index template
...
<$MTBlogName$>
<$MTRandomLine module="formattedquotes"$>
...

2. Random images

You can set up random images in the same way as you set up random quotes. Create a new template module in your Movable Type blog. Fill it with references to your selected images. (Again, make sure to put each image reference on a separate line.)

Template module: "images"
<img src="/images/holidays/rome/rom01.jpg" alt="Me on holiday" />
<img src="/images/costume/medieval/knight.jpg" alt="Me as a knight" />
<img src="/images/funny/chicken01.jpg" alt="Me dressed up as a chicken" />

Then, put the <$MTRandomLine$> tag in the index or archive template where you want the image to appear. (Remember that any < and > characters in the "glue" parameter must be written as their HTML entities (&lt; and &gt; respectively).

Index template
...
<$MTBlogName$>
<$MTRandomLine module="images" num="3" glue="&lt;br /&gt;" $>
...

Just as with quotes, you can put formatting in each line of your "images" module, so that they don't all have to be have the same appearance.

3. Multiple random blog entries

The <$MTRandomLine$> tag can read from files as well as modules. Your index templates write out to files. Put these two together, and what do you get? An option for displying random entries from your blog!

First, create a new index template that will generate a text file with a list of all your blog entries, each on a separate line. Note that I'm using an exceedingly high value for the lastn parameter of MTEntries, to make sure that all of them are listed. You could change this so that, for example, it only selects random entries from the last n entries, or entries from a particular catagory.

Index template: "Bare blog entries", with output file "entries.txt"
<MTEntries lastn="100000">
<a href="<$MTEntryLink$>"><$MTEntryTitle$></a>
</MTEntries>

Then, put the <$MTRandomLine$> tag in the index or archive template where you want the random entry link to appear:

Index template
...
<$MTBlogName$>
<hr />
<h3>Random Entry:<h4>
<p><$MTRandomLine file="/home/username/www/blog/entries.txt" num="3" $></p>
<hr />
...

Important Note: Just because you can do this with MTRandomLine, doesn't mean that you should. Under most circumstances, you'd be better off using David Raynes' MTRandomEntries plugin.

Current version:

Older versions:

1.1 - 23 February 2003

This version adds the parameters "num", "glue", and "allowduplicates". If you want to extract multiple random lines at once, you can now do this with a single instance of the tag instead of having to put the tag in several times. With the "glue" parameter you can specify any text (or HTML) you want to place between each random line. (By default this is blank.) The "allowduplicates" parameter determines whether a line can come up more than once. (By default, no duplicates are returned.)

1.0 - 28 October 2002

This is the "getting it to work" version. It's been a while since I've done any Perl, and it shows. I know there are more efficient to write this code, but it's going to take me some time to figure them out. Future releases will be more optimised.

  • Possibly add an "emit" parameter, with values of "js", "php", "asp", "py", etc. This would automatically generate the necessary script for displaying a new random line whenever someone viewed the page, rather than whenever the page was rebuilt by the Movable Type engine.

For questions, suggestions, bug reports, and anything else related to this plugin, please email MTRandomLine@sunpig.com