Random musings from my awakening dementia...
03.15.2004  
MTKeywordVariable
 

I've been a computer geek since a boy, and thoughts related to computers and software engineering get dropped here for the benefit of humanity and my own hubris.

© 2004-2005, Howard Abrams



Except where otherwise noted, all original content is licensed under a Creative Commons License.
See details.

Does anybody really use that Keyword field in MovableType for the intent it was made? I didn’t, however, I do like the concept of being able to add some meta information to my blog entries that I can then do clever things with.

Case in point… I wanted to be able to specify the width of the displayed text. If I didn’t do anything, I wanted the text in a thin, easier-to-read, column. If I added a second column or a cut-out box (containing a poem or something), then I didn’t want any width.

No problem. Now unless I add a ‘nowidth’ word to my keywords field, I set a default width:

<div class="blog"
  <MTIfNonEmpty tag="MTEntryKeywords">
  <MTElse>
     style="width: 450px"
  </MTElse>
  </MTIfNonEmpty>
  …

Later, I wanted to be able to add an Amazon ID value (effectively an ISBN) to the Keyword field and have every entry that had this value to have a picture of the book or CD and a link to it on Amazon’s web site. Easy enough once I added the MTAmazon plugin:

<MTIfNonEmpty tag="MTEntryKeywords">
  <MTAmazon search="[MTEntryKeywords]" method="Asin">
    <a href="<MTAmazonLink>">
      <img src="<MTAmazonMediumImage>" align="right" border="0">
    </a>
  </MTAmazon>
</MTIfNonEmpty>

So now if I add an Amazon book ID to the Keywords field, it assumes that I want a full width text box… which I may not want. What I want is the ability to add variables to the keyword field and access them in a MovableType template.

So I did what anyone else would do… I wrote a plugin.

With this plugin, I can have as many variables as I like. Something like the following could be added to the Keywords field for a particular entry:

amazon=0142003131 width=450

As you can see, variables are separated from their values by equal signs, and each variable-value pair is separated from one another by spaces. The value can be optional surrounded with quotes (in case the value contains spaces or equal signs). The quotes can either be double or single (if you want to embed a single quote, surround the value in double quotes). For instance, the following is a valid Keyword field content:

byline="That's not it." editor='marsha banks'

You access the value from one of these variables with the following code:

<MTKeywordVariable var="amazon">

Which inserts the value of the variable at the given location. Sometimes it would be nice to only do particular processing if a variable was defined, so we have:

<MTIfKeywordVariable var="amazon">
  Look at Amazon for ID:   <MTKeywordVariable var="amazon">
</MTIfKeywordVariable>

The MTIfKeywordVariable tag has a number of comparison arguments, like ‘equals’, ‘notequals’, ‘lessthan’, etc. For instance:

<MTIfKeywordVariable var="width" greaterthan="100">
  style="width: <MtKeywordVariable var='width'>px"
</MTIfKeywordVariable>

Now, let’s put everything together. My new Amazon code now looks like:

<MTIfKeywordVariable var="amazon">
  <MTAmazon search="[MTKeywordVariable var='amazon']" method="Asin">
    <a href="<MTAmazonLink>">
      <img src="<MTAmazonMediumImage>" align="right" border="0">
    </a>
  </MTAmazon> 
</MTIfKeywordVariable>

Installation

So, if you are still with me, first download KeywordVariable.pl.

Next, upload this file to your MovableType server and put it in the plugins directory (if it doesn’t exist, create the directory first). See MT’s plugin installation instructions.

Next, you need … wait, that is all there is. Just add your plethora of tags to your templates and rebuild.

Feedback

I realize that this is a trivial plugin, but if you put it to use, I’d be curious as to what you are doing with it.

A comment to this from Howard the Author

Doh … I should have assumed that Brad Choate had already addressed this, and in a more general way. I should have done a better job of searching…

His KeyValues plugin does the same thing as this does, but allows you to put the values in any field, not just the keywords.

The only real difference is that my plugin puts the key value pairs on the same line (which works nicely for the Keywords field), and his requires them on separate lines. Yeah, not much of a difference.

Comment posted on Tuesday, 16 March 2004
A comment to this from Seun Osewa

Does your plugin support MTElse?

Comment posted on Monday, 29 March 2004
A comment to this from Kass

Downloaded your plug-in and tried it, but I’m running into a bit of a snag. When I write an entry, I note down what song I’m currently listening to into the keywords field with music=”song title, song artist”.

For the first entry of the day, it’s displaying right… but for the second entry, it’ll display both the first entry’s AND the second entry’s songs.

Was hoping you could tell me what I’m doing wrong? Feel free to check out the URL to see what’s going on or e-mail me for clarification. I’ve only used the MTIfKeywordVariable code on the main index page… and I have MTEntryKeywords displaying first so that you can see what I’m actually typing in the keywords field. Previously I just used the keywords to display the song itself, but I like the idea of being able to expand into other “currently doing blah” things later, if I feel like it.

Thanks in advance… sorry for talking nonstop here.

Comment posted on Monday, 29 March 2004
A comment to this from Howard the Author

Seun…

I assumed that Ben’s <MTElse> tag would just work with any conditional tag made, but it appears that it doesn’t. I suppose I could make an <MTIfNotKeywordVariable> tag…

Kass…

Terribly sorry about the bug, but that’s fixed. Just re-download a plugin again. Hmm… something tells me that I should not be releasing a software fix on the first of April…

Comment posted on Thursday, 1 April 2004
A comment to this from Akos Maroy

I was just wondering how easy would it make to create a plugin, that works similar to the category-based archive listing, but only based on keywords.

instead of having a separate archive page per category, I’d like to have one per keyword. this plugins is the closest to that (though quite far), as it enables a conditional based on a keyword existing or not. But, your plugin requires a keyword to be in key=”value” pair (which is according to your intentions).

I’m just inquring, how difficult would it be to create an tag, that simply checks on a keyword? of course, the best would be an …

Comment posted on Saturday, 4 December 2004
A comment to this from pixeldiva

Thank you, thank you, thank you.

I’ve been banging my head against a brick wall for months trying to get my head around Brad Chaote’s plugin and something about it just didn’t click.

Your plugin seems much simpler and easier to use and finally, I get it. Now I’ll be able to do what I want to do.

Comment posted on Wednesday, 2 March 2005
Another web page that references this entry...
Meow!
Excerpt:Well, a new batch of Movable Type plugins have surfaced. Nothing overwhelmingly exciting this time, but there are a few to keep an eye on. Markdown: I thought all this text-to-HTML stuff was already covered with MT-Textile, but perhaps I'm wrong. While...
Weblog: Tao of Dowingba
Tracked:March 22, 2004 09:56 AM
Another web page that references this entry...
Site Library Mod
Excerpt:If you have had chance to check out our massive media collection in the library, you will have noticed that I link to Amazon.com so that way users can listen to or read excerpts from the items. Amazon has almost...
Weblog: meghan's dolphin
Tracked:October 26, 2004 01:14 PM