Template Tag Modifiers
Template Tag Modifiers are a special kind of template tag that do not exist as a tag literally, e.g. <mt:SomeTagModifier>
. Instead, a template tag modifier is a type of template tag argument that has the ability to transform the contents of the tag it is associated with.
The following are some of the template tag modifiers that Movable Type ships with 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.
Create a plugin module called
Plugin.pm
in the following directory:/path/to/mt/plugins/Example/lib/Example/
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, thenval
will be an ARRAY.ctx
- A reference to the template’s context.