(1) Go to the page in the MT admin interface that you would like to change. Examine the CGI string that got you to that page, in the location bar of your browser. Look for the part that says "__mode=some-action", where "some-action" is something like "menu" or "view". That's the pointer that gets you to the routine that builds the page you're looking at.
(2) Now bring up the file lib/MT/App/CMS.pm in a text editor. Look in the init routine at the top of the file. You'll see a set of pairs 'some-action' => \&some_action. Look for the "some-action" string you found in step 1. The right-hand part of this pair is a reference to the routine that built the page you are looking at. In order to make changes to this page, you will (probably) have to change this routine.
(3) Scroll down in your editor until you find the routine. If you CGI string said "__mode=view", for example, step 2 would have told you that the relevant routine is edit_object. Most of these routines start off something like this:
CODE
sub edit_object {
my $app = shift;
my %param = $_[0] ? %{ $_[0] } : ();
my $q = $app->{query};
The first line grabs the MT application object, which is the first variable passed into edit_object. The second line initializes the %param hash, and the third line grabs an object that knows what got passed to the routine through the CGI string (or through an http POST). It turns out that this object is nothing more or less than the Perl CGI object found in CGI.pm.my $app = shift;
my %param = $_[0] ? %{ $_[0] } : ();
my $q = $app->{query};
(4) If you wish, you can print out the name-value pairs passed from your web browser through CGI to the routine by inserting
CODE
$app->trace(show_query($q));
into your code just after $q is defined. Click the refresh button on your browser and you'll see the contents of your query printed out below the MT page. Be sure to include the following code at the end of CMS.pm as well.CODE
sub show_query {
my $q = shift;
my @records = "\nQuery contents:";
for my $name ($q->param) {
push @records, "\n " . $name . " => " . $q->param($name);
}
push @records, "\n\n";
return @records;
}
my $q = shift;
my @records = "\nQuery contents:";
for my $name ($q->param) {
push @records, "\n " . $name . " => " . $q->param($name);
}
push @records, "\n\n";
return @records;
}
(5) Now scroll down to the end of the routine. If it's edit_object, you'll see
CODE
return $app->build_page("edit_${type}.tmpl", \%param);
there. Again, look in the location bar of your browser to find a "_type=some-type" pair. For edit_object, "some-type" determines the template file to be used. Suppose that "some-type" is "entry", for example. Then the template used will be edit_entry.tmpl. This is the file where you will be able to use your new TMPL_VAR tag.(6) At last we can make our new TMPL_VAR tag. Notice that a reference to the param hash (defined above) is passed to the build_page method. To make your own TMPL_VAR tag, all that is necessary is to create a new entry in this hash. Say, for example, that you want to make a new tag that contains an author's phone number. Then, just above the build_page call, you can say
CODE
$param{author_state_phone} = $phone
and the contents of the variable $phone will appear in the tag CODE
<TMPL_VAR NAME=AUTHOR_STATE_PHONE>
And that's all it takes! Except, of course, defining whatever goes into $phone.
(7) Should you want to see what is getting passed from the routine to the template page, put the following statement in the next-to-last spot in the routine:
CODE
$app->trace(show_params(\%param));
where you've put the show_param routine below at the bottom of the CMS.pm file,CODE
sub show_params {
my $param = shift;
my @records = "\nParam hash contents:";
for my $key (keys %$param) {
push @records, "\n $key => " .
(defined($param->{$key}) ? $param->{$key} : "<undef>");
if (ref $param->{$key} eq "ARRAY") {
push @records, "of length " . @{$param->{$key}};
for my $hash (@{$param->{$key}}) {
push @records, "\n " . $hash . " -->";
for my $key (keys %$hash) {
push @records, "\n $key => $hash->{$key}";
}
}
}
}
push @records, "\n\n";
return @records;
}
my $param = shift;
my @records = "\nParam hash contents:";
for my $key (keys %$param) {
push @records, "\n $key => " .
(defined($param->{$key}) ? $param->{$key} : "<undef>");
if (ref $param->{$key} eq "ARRAY") {
push @records, "of length " . @{$param->{$key}};
for my $hash (@{$param->{$key}}) {
push @records, "\n " . $hash . " -->";
for my $key (keys %$hash) {
push @records, "\n $key => $hash->{$key}";
}
}
}
}
push @records, "\n\n";
return @records;
}
-Rod