Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


Can I have conditional text in a Movable Type template?

Dave, I'm awful curious about how exactly you have the PSP book advert that shows up on your pages related to the Sony PSP, but nowhere else on your site. Is that some fancy trick you're doing with Movable Type, or is that some separate bit of programming wizardry you've done?


Dave's Answer:

I was just explaining this to a friend, actually, so it's convenient that you ask! When I decided that I wanted to have some material show up conditionally, I figured that I could write a simple Perl script that I'd invoke from my individual archive files and that script would look at the page, figure out what category it was in, and then output additional HTML in the case of a match.

Problem was, that's a terrible solution because it's hugely processor intensive and given that I serve up 2-3 pages per second on this server, it'd be a significant burden on the system.

Instead, I dug around in the extensive Movable Type Plug-In Library and came across a plugin called Compare. It lets you have conditional code in your templates based on specific equality tests, including testing for what category or categories match the given entry.

Even better, it's all processed once as the page or pages are rebuilt: because Movable Type creates static pages rather than dynamically generating pages out of a database on demand, it's also far more processor efficient, meaning that each and every visitor has a better experience as their requested page is served up faster. What's not to like about that?

Here's how the code inclusion works. First, just the skeleton:

<MTIfEqual a="[MTEntryCategory]" b="Sony PSP, MP3 Players, Etc.">
  conditional code that matches the specific condition
</MTIfEqual>

You can see that it's pretty straightforward, if a bit awkward. Basically just think "if a = b" and you'll get what's going on.

The final block of code in my actual individual archive template ends up looking like this:

<!-- this is a conditional advert that only shows up with the relevant category -->
<MTIfEqual a="[MTEntryCategory]" b="Sony PSP, MP3 Players, Etc.">

<div class="comments-body"
style="border:1px solid #339;background-color:#eef;padding:10px;color:#333;
margin-bottom:35px;margin-left:25px;margin-right:25px;">

<b>Still Have Sony PSP Questions?</b><br />

Looking for all your Sony PSP answers in one place, organized as a convenient
quick reference work? Then you need <a
href="http://www.askdavetaylor.com/buy/pspbook.html">

<span style="background-color:#ff9;font-weight:bold;">

Dave's Everything Guide to the Sony PSP</span></a>,

150-page ebook ready to download right now!
</div>

</MTIfEqual>

Hope that helps you get everything working on your own blog.


More Useful Blogs and Blogging Articles:
✔   Get my shopping cart plugin to work with WordPress?
We've put in a shopping cart for a client that's not working, and we need some help! The cart is currently using the...
✔   Embed an audio player on a blog or web page?
I have some mp3 audio files I've recorded and would like to have people who visit my site be able to listen to...
✔   Can I write a guest review for AskDaveTaylor.com?
Hi Dave. I'm a big fan of your site and love that you're doing so many reviews now. I've noticed, however, that there...
✔   Change author on WordPress blog post?
I have two accounts set up for my WordPress blog and I'd like to be able to have all my posts from a...
✔   How do I restructure my Wordpress blog without losing SEO?
I have a wordpress blog that was using categories in the url structure like this: /category_name/post_name/ Then I had read somewhere that if...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  




Categorized: Blogs and Blogging   (Article 6802, Written by )
Tagged: blog customization, blog plugins, conditional HTML, movable type
Previous: How do I download music to my RCA Lyra MP3 Player from Mac iTunes?
Next: What are the top baby names?




Reader Comments To Date: 1

Beau Smith said, on July 30, 2008 3:45 AM:

This is now much simpler in MT4.x using the tag:

http://www.movabletype.org/documentation/appendices/tags/ifcategory.html

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.