March 14, 2003
- updated October 01, 2003
MovableType 2.6 ships with a new cgi, called mt-view.cgi, which allows you to dynamically build a page (as opposed to the statical pages that MT creates when it rebuilts an entry). This is similar to how pages are generated using the seach and comment listing CGIs. The mt-view.cgi is not yet officially supported and thus its usage is subject to change. However, it is very useful when you only occasionally need a page and don’t want to store it statically on the server.
mt-view.cgi Examples
The mt-view.cgi can be used to dynamically generate a page. Currently it works for index, archive, category and entry pages. I am only going to describe how to display an entry because that’s what this hack is for.
One feature that is currently missing (as of MT 2.63) is the ability to specify a custom template when building a page dynamically. Right now the mt-view.cgi uses the default template for the entry - not particularly useful if you already build the same page statically. This hack will allow you to specify which template to use when dynamically building an entry.
MovableType/lib/MT/App/Viewer.pm
The following instructions will modified the mt-view.cgi to support specifying which template to use when generating an entry page:
- Open the Viewer.pm file in the lib/MT/App directory in your MovableType installation.
- Find the line that says:
} elsif ($uri =~ m!^/entry/(\d+)/?$!) {
and replace is with the following line:
} elsif ($uri =~ m!^/entry/(\d+)(/.*)?$!) { ## HACK Custom Dynamic Pages
- Find the section that says:
require MT::TemplateMap;
my $map = MT::TemplateMap->load({ archive_type => 'Individual',
blog_id => $app->{__blog_id},
is_preferred => 1 });
my $tmpl = MT::Template->load($map->template_id)
or return $app->error("Can't load template " . $map->template_id);
my $out = $tmpl->build($ctx, \%cond)
or return $app->error("Building archive failed: " . $tmpl->errstr);
$out;
and insert the following code before it:
## BEGIN HACK Custom Dynamic Pages
## http://www.nonplus.net/software/mt/CustomDynamicPages.htm
# Check whether custom template was specified
if($app->path_info =~ m!^/\d+/entry/\d+/([^/]+)$!) {
# Try loading, first by ID, then by name
my $tmpl = MT::Template->load($1)
|| MT::Template->load( { blog_id => $app->{__blog_id}, name => $1 } );
return $tmpl->build($ctx, \%cond)
or $app->error("Building archive failed: " . $tmpl->errstr) if $tmpl;
}
## END HACK Custom Dynamic Pages
Example
Let’s say you want to dynamically create print preview pages for your entries. After applying this hack, you would use the following steps:
- Create a template
Create a new Archive Template in MovableType and give it a descriptive Template Name, say print_entry. We are going to be using this as an invidivual entry archive, so you can use all the usual MT tags you’d expect.
- Link to the dynamic page
In other templates, link to the dynamically created page by specifying the Template Name like so:
<a href="<$MTCGIPath$>mt-view.cgi/<$MTBlogID$>/entry/<$MTEntryID$>/print_entry"
title="Print <MTEntryTitle escape_html="1">">Printable Version</a>
That’s it! Whenever someone visits the “Printable Version” link, MovableType will recreate the page using the print_entry template.