"Shortcodes" in Movable Type/Melody
January 25, 2010
0 comments
The shortcode feature of WordPress is very similar to the template tags in Movable Type. The idea behind shortcodes is that you could create shortcuts that will translate a markup command like [copyright] into a function call to a PHP function that would supply the copyright text for your blog, or a markup command like [pullquotes][/pullquotes] to wrap pull quotes around a body of text.
To create a simple function tag that would work like that, you would first create a Movable Type plugin that defines it. Movable Type 4.X and 5.X plugins reside in the plugins directory of Movable Type in their own subfolder. For this plugin, a config.yaml file and a Perl module would be needed. The directory structure will end up looking like the following example where MT_DIR is the directory where Movable Type is installed
MT_DIR/plugins/
MT_DIR/plugins/CopyrightShortcode
MT_DIR/plugins/CopyrightShortcode/config.yaml
MT_DIR/plugins/CopyrightShortcode/lib/CopyrightShortcode.pm
The config.yaml file uses four spaces to denote a new block; anything other than four spaces, such as tabs, are invalid a YAML file. The following is the minimal YAML needed to define this plugin:
Now, the minimal amount of Perl needed to implement this in the CopyrightShortcode.pm Perl module would be:
That will generate a new tag that can be called in any template as <$mt:CopyrightNotice$>. A more complicated example, such as the "pullquote example" from this article could be created with block tags. For example, a function that would create a <mt:PullQuote></mt:PullQuote> tag would look like this:
The only caveat to this approach is that Movable Type rich text editor does not understand Movable Type markup, therefore you will have to either change it to source view and carefully add it to the post, making sure that it doesn't attempt to escape the < and > characters or change the text editor to use no formatting so that you can completely control the HTML and Movable Type markup that are put into it.
With that said, the general principles behind WordPress shortcodes can be applied anywhere in Movable Type via a function tag. You could call that CopyrightNotice tag anywhere in a template just like you could call an equivalent shortcode in WordPress. The only area where you may not want to do this would be in comments, as mteval could be used to do things like allow a malicious commenter to call Movable Type system tags which could be used to display information about your blog that would normally not be exposed such as the email addresses of all registered users. However, what they allow registered, privileged authors to do in the text of their own posts is quite powerful, especially when the users are able to add their own plugins.
To create a simple function tag that would work like that, you would first create a Movable Type plugin that defines it. Movable Type 4.X and 5.X plugins reside in the plugins directory of Movable Type in their own subfolder. For this plugin, a config.yaml file and a Perl module would be needed. The directory structure will end up looking like the following example where MT_DIR is the directory where Movable Type is installed
MT_DIR/plugins/
MT_DIR/plugins/CopyrightShortcode
MT_DIR/plugins/CopyrightShortcode/config.yaml
MT_DIR/plugins/CopyrightShortcode/lib/CopyrightShortcode.pm
The config.yaml file uses four spaces to denote a new block; anything other than four spaces, such as tabs, are invalid a YAML file. The following is the minimal YAML needed to define this plugin:
id: CopyrightShortcode
key: CopyrightShortcode
name: CopyrightShortcode
description: Provides a simple function that returns a copyright notice
tags:
function:
CopyrightNotice: CopyrightShortcode::_hdlr_copyright_notice
Now, the minimal amount of Perl needed to implement this in the CopyrightShortcode.pm Perl module would be:
package CopyrightShortcode;
use strict;
sub _hdlr_copyright_notice
{
return "Copyright 2010 John Smith. All rights reserved."
}
1;
That will generate a new tag that can be called in any template as <$mt:CopyrightNotice$>. A more complicated example, such as the "pullquote example" from this article could be created with block tags. For example, a function that would create a <mt:PullQuote></mt:PullQuote> tag would look like this:
sub _hdlr_pullquotesThe only addition needed to the config.yaml file to add that to the Movable Type/Melody registry would be:
{
my ($ctx, $args, $cond) = @_;
#$ctx->slurp tells the MT template interpeter
#to interpret the contents of the block tag
#and return them as a string.
my $out = $ctx->slurp($args, $cond) or return '';
return "<blockquote class='pullquote'>$out</blockquote>";
}
tags:Now, suppose that you wanted to create a tag that could be included in the actual body of a post. Many users find that Movable Type does not seem to support the ability to write a post and arbitrarily throw in Movable Type markup into a post, but that is not true. There is a template tag modifier called mteval which will make the template interpreter look for Movable Type markup inside of the content returned by a function tag. Therefore, if you add mteval="1" to <$mt:EntryBody$> it will execute the Movable Type markup inside a blog post.
block:
PullQuote: PullQuotePerlModuleName::_hdlr_pullquotes
The only caveat to this approach is that Movable Type rich text editor does not understand Movable Type markup, therefore you will have to either change it to source view and carefully add it to the post, making sure that it doesn't attempt to escape the < and > characters or change the text editor to use no formatting so that you can completely control the HTML and Movable Type markup that are put into it.
With that said, the general principles behind WordPress shortcodes can be applied anywhere in Movable Type via a function tag. You could call that CopyrightNotice tag anywhere in a template just like you could call an equivalent shortcode in WordPress. The only area where you may not want to do this would be in comments, as mteval could be used to do things like allow a malicious commenter to call Movable Type system tags which could be used to display information about your blog that would normally not be exposed such as the email addresses of all registered users. However, what they allow registered, privileged authors to do in the text of their own posts is quite powerful, especially when the users are able to add their own plugins.
