Variables and Conditional Display

Two of the most powerful and probably the least widely used tags in Movable Type are MTSetVar and MTGetVar. These tags allow you to set and get a variable like so

<MTSetVar name="foo" value="bar">
<MTGetVar name="foo">

and can be used for a great variety of uses. If you use PHP, this can be done in a much easier way like so

<?php $foo = "bar"; ?>

and you can get the value of that variable by just using $foo within a block of PHP code (this method is more powerful than using MTSet/GetVar because you can use MT tags within PHP.)

Using MTSet/GetVar (or manually setting the variable in PHP) has several uses. Elise covered one of them with her tutorial on font colors but you can use them in several ways, especially in cases in which you would like a conditional display. A conditional display is a visual display that changes when certain conditions occur.

For example, one of the ways that I used a conditional display is with the tab bar at the top of Movalog. The tab bar is in a file called header.php and this file is included at the top of every page. The appropriate tab is highlighted using conditional display, like so:

<li<?php if($site == "Movalog") { echo ' id="active"'; } ?>><a href="http://www.movalog.com/">Movalog</a></li>

The above PHP basically checks a variable called $site and if that equals "Movalog", the tab is highlighted. Therefore, to get the tab to be highlighted I first set the variable $site and then include the file that does the conditional display. You must set the variable before testing for it., like so

<li<?php if($site == "Movalog") { echo ' id="active"'; } ?>><a href="http://www.movalog.com/">Movalog</a></li>

<?php $site = "Movalog";
include("header.php");

1 Comments

Charles said:
on Aug 28, 2005 11:06 AM | Reply

Can these be used to pass the variables to other pages?

I have tried to pass variables but have not been able to read them into the MT tags.

Thanks

-Charles