MT::Template - Movable Type template record


    use MT::Template;
    my $tmpl = MT::Template->load($tmpl_id);
    defined(my $html = $tmpl->build($ctx))
        or die $tmpl->errstr;

    $tmpl->name('New Template name');
        or die $tmpl->errstr;


An MT::Template object represents a template in the Movable Type system. It contains the actual template body, along with metadata used for keeping the template in sync with a linked file, etc. It also contains the functionality necessary to build an output file from a generic template.

Linking a template to an external file means that any updates to the template through the Movable Type CMS will be synced automatically to the file on disk, and vice versa. This allows authors to edit their templates in an external editor that supports FTP, which is preferable for users who do not like editing in textareas.


As a subclass of MT::Object, MT::Template inherits all of the data-management and -storage methods from that class; thus you should look at the MT::Object documentation for details about creating a new object, loading an existing object, saving an object, etc.

The following methods are unique to the MT::Template interface:

$tmpl->build($ctx [, \%cond ])

Given a context $ctx (an MT::Template::Context object) and an optional set of conditions \%cond, builds a template into its output form. The template is first parsed into a list of tokens, then is interpreted/executed to generate the final output.

If specified, \%cond should be a reference to a hash with MT tag names (without the leading MT) as the keys, and boolean flags as the values--the flags specify whether the template interpreter should include any conditional containers found in the template body.

Returns the output as a scalar string, undef on error. Because the empty string '' and the value 0 are both valid return values for this method, you should check specifically for undef:

    defined(my $html = $tmpl->build($ctx))
        or die $tmpl->errstr;


Only applicable if the template is an Index Template, this method returns the url for the page which is the result of building the index template. If the template is not of type index, or if the index template has not built yet (if the template is static), or if the index template does not have corresponding FileInfo record (if the template is dynamic), this method returns undef.


Returns the MT::Blog object associated with the template.


Saves the template and if linked to a physical file, updates the file.


Removes the template object and any related objects in MT::TemplateMap and MT::FileInfo.


Updates the values of the $tmpl object. When this is called through the MT::ObjectDriver classes (upon loading a template object), and if the template is linked to a file, it will also load the contents of that file, setting the 'text' property.


The MT::Template object holds the following pieces of data. These fields can be accessed and set using the standard data access methods described in the MT::Object documentation.

  • id

    The numeric ID of the template.

  • blog_id

    The numeric ID of the blog to which this template belongs.

  • name

    The name of the template. This should be unique on a per-blog basis, because templates--particularly template modules, which are stored as regular templates--are included by name, using <MTInclude>.

  • type

    The type of the template. This can be any of the following values: index, an Index Template; archive, an Archive Template; category, also an Archive Template; individual, also an Archive Template; comments, a Comment Listing Template; comment_preview, a Comment Preview Template; comment_error, a Comment Error Template; popup_image, an Uploaded Image Popup Template; or custom, a Template Module.

  • outfile

    The name/path of the output file of this template. This is only applicable if the template is an Index Template.

  • text

    The body of the template, containing the markup and Movable Type template tags.

    If the template is linked to an external file, the body of the template is automatically synced between this data field and the external file.

  • rebuild_me

    A boolean flag specifying whether or not the index template will be rebuilt automatically when rebuilding indexes, rebuilding all, or saving a new entry.

  • linked_file

    The name/path of the file to which this template is linked in the filesystem, if it is linked.

  • linked_file_mtime

    The last modified time of the linked file. This, along with linked_file_size, is used to determine whether a file has been updated on disk, and needs to be re-synced.

  • linked_file_size

    The size of the linked file, in bytes. This, along with linked_file_mtime, is used to determine whether a file has been updated on disk, and needs to be re-synced.


In addition to numeric ID lookup, you can look up or sort records by any combination of the following fields. See the load documentation in MT::Object for more information.

  • blog_id
  • name
  • type


  • When you remove a template using MT::Template::remove, in addition to removing the template record, all of the MT::TemplateMap objects associated with this template will be removed.
  • If a template is linked to an external file, MT::Template::save will sync the template body to disk, and MT::Template::text (the data access method to retrieve the body of the template) will sync the template body from disk.


Please see the MT manpage for author, copyright, and license information.