Hacks : Custom Dynamic Pages 1.2 Mar 14 2003 | # | C:18

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.

  • Generating an Entry
    The format of the URL is http://www.my.site.com/mt/mt-view.cgi/BlogId/entry/EntryID. For example, the current page can be dynamically generated using this URL: http://www.nonplus.net/mt/mt-view.cgi/2/entry/595. If you create a dynamic link to an entry in an MT template, you could do it like so:
    <a href="<$MTCGIPath$>mt-view.cgi/<$MTBlogID$>/entry/<$MTEntryID$>">Dynamic Entry</a>
    

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:

  1. Open the Viewer.pm file in the lib/MT/App directory in your MovableType installation.
  2. 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
    
  3. 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
    
  4. 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:

    1. 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.
    2. 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.

Version History
  • Version 1.2 - July 7, 2003
    Fixed to limit search for the template name to the specified blog.
  • Version 1.1 - May 1, 2003
    Simplified instructions and corrected a mistake (there was an extra “}” in the second section to find).
  • Version 1.0 - March 14, 2003
    Initial release
The site database appears to be down.