Attributes: Template Tag Modifiers

Template Tag Modifiers are attributes that are applied to constrain or transform the information returned by a template tag.

Some of the template tag modifiers that Movable Type supports by default:

  • capitalize - convert all the characters output by the tag to uppercase.

  • replace - perform a simple string substitution on the contents of the tag.

  • word_count - instead of returning the contents directly, return an integer reflecting the number of words contained by the tag.

  • ltrim and rtrim - remove white space from the left/right of the tag's contents.

And many more of course. Here are some examples using some of the tags above:

<mt:Entries id="40" capitalize="1"><mt:EntryBody></mt:Entries>

<mt:Var name="foo" replace="Byrne Reese","BR">

The following reference will walk you through the process of defining your own template tag modifiers.

Registering Your Tag Modifier

As with any plugin feature, the first step is always a visit to the config.yaml. Here is a sample config.yaml file for declaring a template tag modifier:

name: Example Plugin for Movable Type
id: Example
description: This plugin is an example plugin for Movable Type.
version: 1.0
tags:
    modifier:
    lolcats:
        handler: $Example::Example::Plugin::lolcats

Defining Your Tag Modifier's Behavior

Once the tags have been declared in your config.yaml it is time to write the code that will govern their behavior.

  1. Create a plugin module called Plugin.pm in the following directory:

    /path/to/mt/plugins/Example/lib/Example/

  2. Edit Plugin.pm and cut and paste the following into it using a text editor:

    package Example::Plugin;
    use strict;
    
    
    sub lolcats {
        my ($str, $val, $ctx) = @_;
        return "LOL - CAN I HAZ A $str";
    }
    
    
    1; # Every module must return true
    

When a template tag modifier is invoked, Movable Type will pass three arguments to the handler. They are:

  • str - The value of the template tag's content.

  • val - The value passed into the global modifier attribute. If multiple values are passed, then val will be an ARRAY.

  • ctx - A reference to the template's context.

This page was last updated on 2009-01-03, 12:57.  

Leave a note

Have a question, please use the MT Forums. Notes sumbitted here should pertain to tips & hints regarding documentation. Your note may be removed once it's contents has be integrated into the body of the page.