Loading Alternate Templates Based upon UserAgent
Every time a page is requested from a Web server, the requesting client transmits information about itself, including the name of the browser making the request, the operating system being used and any additional version information that might be helpful.
The iMT plugin which provides a specially designed user interface for users of Apple’s iPhone makes use of this information to modify the search path for templates in real time based upon the name of the client making the request to Movable Type.
Using init_request
to change your alt-tmpl search path
Movable Type plugins can register an init_request
callback that is invoked during the initialization phase of the application. This allows plugins to intercept any request to the application for special handling.
In this case, the init_request
callback is used to detect if the device accessing Movable Type is an iPhone, and if so, to prepend to the template search path the location of the plugin’s alt templates.
First, register your callback:
name: Example Plugin for Movable Type
id: Example
description: This plugin is an example plugin for Movable Type.
version: 1.0
callbacks:
init_request: $Example::Example::Plugin::init_request
Then you need the callback handler itself.
sub init_request {
my $plugin = shift;
my ($app) = @_;
$enabled = 0;
if ($app->isa('MT::App::CMS')) {
if (my $ua = $ENV{HTTP_USER_AGENT}) {
if (( $ua =~ m!AppleWebKit/! ) && ( $ua =~ m!Mobile/! )) {
$enabled = 1;
# Redirect 'dashboard' or 'default' modes to iphone_main
$app->mode('iphone_main')
if ($app->mode eq 'default') || ($app->mode eq 'dashboard');
$app->config('AltTemplatePath', $plugin->path . '/tmpl');
}
}
}
}