Not a developer? Go to MovableType.com

Documentation

Template Tags in Movable Type

Template tags are placeholders for variable content that's stored in Movable Type. During the publishing process each template tag is responsible for printing out a particular piece of information. In addition, template tags are contextual, meaning that the information that they display is tightly bound to the placement of the tag itself.

Learn more: This document covers the basics of the template tag model. The template tags themselves are listed and described in full in the Appendix.

Template Tag Syntax

For those who are familiar with HTML and XML, Movable Type template tags will seem quite familiar. All template tags are enclosed within less-than and greater-than signs, sometimes referred to as angle brackets. Container end tags have a forward slash that is located after the less-than symbol, but before the tag name.

Template tags also use prefixes, also known as namespaces, to clearly differentiate them from HTML and other markup languages you might be using like PHP, ASP, JSP, etc. For example:

<mt:entries>
  <mt:entrytitle />
</mt:entries>

Template tags are case INSENSITIVE, allowing designers to choose the capitalization scheme they most prefer. For example, the following tags are all equivalent:

<mt:FooBar>
<mt:foobar>
<MT:fooBar>

It is recommended that users use the syntax described above as going forward all of our documentation will also use this syntax. However, for backwards compatibility purposes Movable Type supports legacy template tag syntaxes. For example, the following code fragments are both equally supported:

<mt:entries><mt:entrytitle /></mt:entries>

<MTEntries><$MTEntryTitle$></MTEntries>

Template Tag Attributes

Some template tags can take attributes that modify the standard behavior of a tag. An attribute is another syntactic feature of Movable Type tags that are familiar to those that have previously worked with HTML and XML. Attributes are key/value pairs in the angle brackets that follow the tag name and are separated by spaces. Values are always enclosed in double or single quotes. For instance:

<mt:entries lastn="40">
<mt:entrytitle />
</mt:entries>

In this example lastn is an attribute of the container tag mtentries with a value of 40. The lastn modifies the behavior of mtentries by listing the last 40 entries instead of the default (as configured in the weblog's display settings). Many template tags have optional attributes and some even have required attributes.

Additionally, Movable Type provides a set of global filter attributes that can be used on all tags which output content.

Template Tag Types

Below we will cover the three classes of template tags (Variable, Container and Conditional) and help you understand how they are used with respect to syntax and context.

Variable Tags

Variable tags are simple placeholders that represent a single piece of content like the name of the weblog or the title of a particular entry. For example:

<mt:blogname />
<mt:entrytitle />

Container Tags

Container tags get their name because they contain other tags and text and are comprised of both start and end tags (sometimes referred to as a tagset).

Container tags typically provide a list of things to iterate over and the contents inside of such a tag would be evaluated repeatedly. For instance:

<mt:entries>
"<mt:entrytitle />" was written by <mt:entryauthor /> on
<mt:entrydate />
</mt:entries>

In the example above, mtentries is the container tag and it iteratively lists entries. For each entry, the variable tags inside of the container outputs the title of the entry, its user and the date on which it was published.

Conditional Tags

A third type of template tag, called a conditional tag, is a special form of container tag that does not iterate, but simply outputs its contents if and only if the particular conditional statement it represents is true.

<mt:ifcommentsmoderated>
Comments are moderated.
</mt:ifcommentsmoderated>

In the example above, the text "Comments are moderated" are printed if comments on the weblog are moderated.

Conditional tags can be easily recognized because their names always start with mt:if

Template Tag Context

It's necessary to have a basic understanding of template tag types and their functions in the template engine in order to understand what we mean by tag context. Tag context is an extremely important concept which provides the location for the way in which the publishing system works.

Context refers to the interrelated conditions of a tag's position and usage within the template and relationship to other tags surrounding it. The context of a tag defines its behavior, state, and even purpose during the processing of a template. Take this template markup for example:

<mt:entries>
<mt:entrytitle /> <!-- in entry context. -->
</mt:entries>
<mt:entrytitle /> <!-- ERROR! out of context. -->

The first occurrence of MTEntryTitle is said to be in the context of mtentries. As previously discussed, MTEntries represents a list of entries that the template engine will loop through, outputting the title with each pass. The second occurrence in this example will generate an error because it is out of the context of MTEntries; the template engine does not know what entry to use to display the title.

Explicit vs. Implicit Context

The example used above is an example of explicit context. A container tag, in this case mtentries, is explicitly creating an entry context for all of the content it contains. But mtentries also must have a context in order to know which entries to list. This is called an implicit context and is provided by both the template containing the tag, the weblog itself and even the system as a whole.

So, for example, while the mtentries container tag will iterate over the most recent entries on a weblog in the main index template, it provides a list of all entries in a particular category on a category archive page. Of course, the same tag in the same templates on another weblog would list entirely different entries using that weblog as the source.

Each of these contexts, implicit and explicit, cascade down and combine to form the final context used to evaluate the variable tags which output weblog data. This contextual modification is important because it allows you in almost every case to use the same exact code in different templates without having to worry about including extra instructions for the system about what to output.

See Also

» Next: Template Types

Back

1 Comment

coolpillows

coolpillows on June 11, 2009, 9:47 p.m. Reply

I would love to see a better explanation of context in plain language. From what it says here:

“It’s necessary to have a basic understanding of template tag types and their functions in the template engine in order to understand what we mean by tag context. Tag context is an extremely important concept which provides the location for the way in which the publishing system works.”

That sounds like the key the MT universe. Further:

“…context…is provided by both the template containing the tag, the weblog itself and even the system as a whole.”

and finally:

“contexts…cascade down and combine to form the final context used to evaluate the variable tags which output weblog data.”

This sounds revealing, but doesn’t explain further other than on the development side where context is broken down in Perl. Is there any other documentation out there that explains this?

Leave a Comment