Creating a New Screen Inside the Application
To create a new screen in the Movable Type application a developer needs to do three things:
- Create a template for rendering the contents of the page
- Define a mode handler which populates the template's context with data
- Register a method to route requests to the mode handler
Creating a Template
The templating language used by the blogs published by Movable Type is the same language used for rendering pages within the application. This templating language provides a rich set of template tags that abstract developers and designers away from the core application using a customizable, but still rigid set of semantics that make Movable Type more secure and less error prone. It also ensure thats any new page that is creating using this language will work equally well on any operating system and using any database.
Users familiar with Smarty, JSP, ASP, PHP or any other popular web based templating framework should feel at home using Movable Type's templating language that allows developers to easily interweave the programattic display of data with HTML.
Here is an example Movable Type application template:
<mt:setvarblock name="page_title"><__trans phrase="This is a page title"></mt:setvarblock>
<mt:setvarblock name="html_head" append="1">
<script type="text/javascript">
<!--
function do_something(f) {
alert("Something!");
}
// -->
</script>
</mt:setvarblock>
<mt:include name="include/header.tmpl">
<form method="post" enctype="multipart/form-data"
action="<mt:var name="script_url">">
<input type="hidden" name="__mode" value="a_mode" />
<mt:if name="blog_id">
<input type="hidden" name="blog_id" value="<mt:var name="blog_id">" />
</mt:if>
<input type="hidden" name="magic_token" value="<mt:var name="magic_token">" />
<mtapp:setting
id="some_id_field"
label="<__trans phrase="Enter text here">"
hint="<__trans phrase="Yay, text.">"
content_class="field-content-text">
<input type="text" name="foo" size="30" />
</mtapp:setting>
<mt:setvarblock name="action_buttons">
<button
type="submit"
accesskey="s"
title="<__trans phrase="Continue (s)">"
class="primary-button"
><__trans phrase="Continue"></button>
</mt:setvarblock>
<mt:include name="include/actions_bar.tmpl" bar_position="bottom"
hide_pager="1" settings_bar="1">
</form>
<mt:include name="include/footer.tmpl">
This template demonstrates a number of different user interface components that anyone is free to use to make creating a user interface simpler:
<mtapp:setting>- used for rendering form and input elements in a standard way<mtapp:button>- used for rendering a Movable Type HTML button<mt:include>- used to include HTML and template fragments contained in other files and templates<mt:if>- a simple conditional statement<mt:setvarblock name="html_head" append="1">- a way of including javascript at the head of your page
Define a Mode Handler
Once you have created a template you need to define a mode handler which will be responsible for loading the template, populating its context with parameter input and data, and then returning its HTML. The HTML returned by your handler will be displayed directly to the user's browser.
It is recommended to use the $app->build_page method to assist in both loading and processing your template. It will handle error reporting and what not for you.
Here is an example CMS package you can include within a plugin that will process incoming requests for a single mode.
package MyPlugin::CMS;
use strict;
use base qw( MT::App );
sub plugin {
return MT->component('MyPlugin');
}
sub id { 'myplugin_cms' }
sub some_mode {
my $app = shift;
my $input = $app->{query}->param('some_form_parameter');
my $plugin = MT::Plugin::MyPlugin->instance;
my $tmpl = $plugin->load_tmpl('some_template.tmpl');
return $app->build_page( $tmpl, $param );}
}
Register Your Mode Handler
Ok, so you have a template and a mode handler for displaying and processing the template to the user. You now need to instruct MT on how to dispatch requests to this handler. This is done by registering a "mode" within MT's registry, like so:
sub init_registry {
my $plugin = shift;
$plugin->registry({
applications => {
cms => {
methods => {
my_mode =>
'$MyPlugin::MyPlugin::CMS::some_mode',
},
},
},
});
}
See also:
- Creating a New Screen in the Application
- Creating Menu Items
- Creating List Filters of "Quick Filters"
- Template Tag Reference Manual
- User Interface Components Reference

Mark Carey
August 23, 2007 5:28 AM | Reply
In the first section above, the example shows <button> -- should this be <mtapp:button>?
Chad Everett
August 31, 2007 5:27 AM | Reply
And actually, if you are using the $app->listing method, you don't need to use the include/actions_bar item either - it will be created automatically, as long as there are actions assigned to the object.
chiggins
November 14, 2007 3:04 PM | Reply
I'm assuming that:
template goes in: [plugin-dir]/MyPlugin/tmpl/some_template.tmpl
mode handler goes in: [plugin-dir]/MyPlugin/lib/MyPlugin.pm
and that init_registry goes in: [plugin-dir]/MyPlugin/MyPlugin.pl
Also, in the mode handler:
sub some_mode { my $app = shift; my $input = $app->{query}->param('some_form_parameter'); my $plugin = MT::Plugin::MyPlugin->instance; my $tmpl = $plugin->load_tmpl('some_template.tmpl'); return $app->build_page( $tmpl, $param );} }Where did $param come from in the call to $app->build_page()? Should that be $input?
chiggins
November 15, 2007 2:39 PM | Reply
Okay, I've been over this over and over again.
What is the relationship between these parts?
the form's _mode value is 'amode', the handler is MyPlugin::CMS::somemode, and the mode in the registry is 'mymode'. Not only are these names not meaningful, their relationship isn't clear.
The handler class is MyPlugin::CMS, the plugin class is MT::Plugin::MyPlugin which doesn't seem to be referenced (or linked to another tutorial?), the reference to the mode handler is '$MyPlugin::MyPlugin::CMS::some_mode' (is that a mistake, or is there a reason for that?)
I don't mean to seem unappreciative for the effort, but 'foo' examples without so much as comments in the code just aren't that helpful. Any chance y'all could work up a stupid applied example for dense developers like myself who are trying to get going quick and extrapolate examples, perhaps the tried-and-true CD/Book library example, and comment the code copiously?
Apologize if that's harsh, and thank you.
(Also, despite my best efforts at wrapping anything with an underscore in 'code' tags, the comments keep coming up with underlined things in italics. lame and frustrating.)
flowagner
March 1, 2008 5:35 AM | Reply
@Mark Carey: They actually use the standard html button element. So it's just <button>.
But to display the buttons on the example page one may have to add the following element at the beginning of the template:
<mt:setvar name="position(underscore)actions(underscore)bottom" value="1"> (Replace (underscore) with _. Don't know how to escape the underscore...).
Otherwise MT might simply hide the div containing the button.