November 19, 2003

Using SmartBody

I'm working on a site using MT for (among other things) a client's news blog. Today's dilemma was that entries would be either internal PR pieces or synopses of external mentions with a link, and I wanted to allocate one of the fields as a place where the end user would simply put the URL for the external story, then, in the template, have an href pointing to <$MTThatField$> only if that field had been populated.

My first thought was to use the extended entry field, because there's that handy IfExtended tag. Of course that didn't work, because we have to leave the entries set to 'convert line breaks' for the non-tech end users who don't know how to make tags (and that's okay; they pay me to do it for them), which resulted in hrefs pointing to "<p>http://kalsey.com/2002/07/smartbody_plugin/</p>".

My second idea was to use the excerpt field because it doesn't create breaks. All I needed was an on/off switch. I googled 'IfExcerpt' and came across Adam Kalsey's SmartBody plugin, which worked out perfectly, after a few mishaps.

What I started out with:

<$MTEntryBody$>
<MTEntrySmartBody>
<$MTEntryIfExcerpt$>
<a href="<$MTEntryExcerpt$>">Click here to read the full story</a>
</MTEntryIfExcerpt>
The first problem was one already mentioned in the comments -- that when you use the MTEntrySmartBody tag it overrides the 'convert line breaks' and removes them from all the fields and leaves you with one big, ugly paragraph.
Paragraph 1 Paragraph 2
rather than
Paragraph 1
Paragraph 2
Kalsey suggests using Brad Choate's format breaks plugin, but I decided to try something else first.

Turns out that using a close tag -- </MTEntrySmartBody> -- lets the line breaks show up in the other fields.

<$MTEntryBody$>
<MTEntrySmartBody>
<$MTEntryIfExcerpt$>
<a href="<$MTEntryExcerpt$>">Click here to read the full story</a>
</MTEntryIfExcerpt>
</MTEntrySmartBody>
The problem now was that, while it was making a correct link and breaking where I wanted it to, it was also printing the full link URL above the link text:
Paragraph 1
Paragraph 2
http://kalsey.com/2002/07/smartbody_plugin/
Click here to read the full story

I still have no idea why this was happening. I tried several things, none of which worked, but eventually I tried removing the MTEntrySmartBody tags altogether et voila -- works perfectly, on both the index and in the archives.
<$MTEntryBody$>
<$MTEntryIfExcerpt$>
<a href="<$MTEntryExcerpt$>">Click here to read the full story</a>
</MTEntryIfExcerpt>
results in:
Paragraph 1
Paragraph 2
Click here to read the full story
(I should probably note here that I'm using MT 2.64 and have no idea if any of this is true with older versions.)

I also modified the SmartBody plugin to include an IfTitle tag, which works just like MTEntryIfExcerpt, except you use MTEntryIfTitle. See Kalsey's site for installation instructions.

download .zip file

Examples:
Algonquin in the News
Don Harlow Reviews -- IfTitle for thumbnail, IfExcerpt for trailer link, IfExtended for pull quote

Posted by gwen at November 19, 2003 06:53 PM | TrackBack
Comments