Chapter 3: Templates and Layout
Introduction
One of Movable Type's most powerful traits is its flexibility to adapt to practically any weblog design or use you can imagine. For the average user, most of this flexibility comes from Movable Type's template engine.
The template engine is crucial to automating the process of publishing and is what makes a publishing system go. When a rebuild is performed, templates are merged with content to create a page that visitors can view in their browsers.
These templates are what control the design and layout of your site and what keeps that design separate from the content. Templates describe where you want to put your content and what that content should look like, using special markers (tags) that are typically mixed with another markup language such as HTML or XML.
The flexibility and power stem from the fact that with MT templates and tags, you don't need to know or understand programming code in order to customize what gets generated by the system.
Template & Tag Philosophy
There are two main ideas behind Movable Type's template system.
- Container tags represent lists, contexts, and conditionals. Variable tags are placeholders that represent a piece of content or data.
- Context is important in determining when you can use a particular tag and how it will behave.
Tag Types
Movable Type template tags can be classified as two types.
Variable Tags
Variable tags are placeholders that represent a piece of content or data. A tag of this type will be substituted with a value based on the context of its use. For instance:
<$MTEntryTitle$>
MTEntryTitle a variable tag that will be replaced with the
title of an entry.
Container Tags
Container tags are start and end tags (sometimes referred to as a tagset) that represent lists, contexts, and conditionals. These tags get their name because they usually contain other tags and text. For instance:
<MTEntries>
<$MTEntryTitle$>
</MTEntries>
MTEntries is a container tag that lists entries. For each
entry the container lists will output the title of the entry.
There is a special type of container tag called a conditional tag. Conditional tags will only display their contents if a certain condition is true.
<MTEntryIfExtended><$MTEntryTitle$> is extended.</MTEntryIfExtended>
In the example above, the entry title and the text "is extended" will only be displayed if there is content in the Extended Body field of the entry.
Container tags can also be used to create a particular context. A few examples:
- MTEntryPrevious and MTEntryNext each impose an entry context for the entries adjacent to the current entry.
- MTArchivePrevious and MTArchiveNext do the same, except for archives and an archive context.
- MTParentCategory provides a context for the parent of the current category in context
Context
The term context has been briefly mentioned already and is one you will encounter often when working with Movable Type's template tags.
Up until now, without some understanding of MT tag types and their functions in the template engine, it would have been difficult to precisely explain what is meant by the term in MT.
Context refers to the interrelated conditions of a tag's position, usage, 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 MT template markup for example:
<MTEntries>
<$MTEntryTitle$> <!-- in entry context. -->
</MTEntries>
<$MTEntryTitle$> <!-- 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.
Be aware that context is not always defined by tags. The template engine will create contexts before processing any tags. This "tagless" form of context is called an implied context, meaning it will vary depending on the template type. This is a something that will be discussed along with template types.
Tagging Principles
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.
All template tag names are prefixed with "MT," to separate template tags from other markup tags. To further separate them, an optional dollar sign inside the template tag angle brackets can be added. Here is an example of a template tag:
<$MTEntryBody$>
While the dollar signs are optional, it is highly recommended that they be used on all variable tags except for container tags.
<MTEntries>
<$MTEntryTitle$>
</MTEntries>
This convention is used throughout the default templates and in this documentation.
The documentation says the parser cannot find the end tag if the $ is used on containers. Is this still true?
Some template tags can take attributes that modify the standard behavior of a tag. An attribute is another syntactic feature of MT tags that will be 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:
<MTEntries lastn="40">
<$MTEntryTitle$>
</MTEntries>
In this example lastn is an attribute of the container tag
MTEntries with a value of 40. The lastn modifies the
behaviour of MTEntries by listing the last 40 entries instead
of the default, as configured in the weblog's display settings.
Template Types
The template type is significant in MT, because it defines how the system is to use the template, and the context it will be given. Different types include:
- Index Templates
Index Templates will create a single page of content from the weblog. Index Templates are commonly manifested as a list of entries; however this is not explicitly required. For instance, the default templates that ship with MT include a Style sheet template that does not list any entries -- in fact it doesn't have any MT template tags at all. (It is included as a template for convenient access.)
Index Templates have an implied context of the current weblog.
- Archive Templates
In Movable Type, an archive is the entire collection of weblog entries. Archive Templates allow for pages to be created for different views of the weblog's entries. MT supports a few date-based archives -- daily, weekly, and monthly -- in addition to individual entry and category archives. These templates are used to create multiple pages, one for each "unit" (time-period, entry or category) of the archive.
Archive Templates have an implied context of a selection of entries for a particular time-period or category, within the current weblog. Individual Entry Archives have an implied context of one particular entry within the current weblog. Unlike date-based and category-based archives, a
MTEntriestag is not needed. - System Templates
System templates are always created dynamically and are associated with a specific application task. The purpose of these templates vary, but are generally tied to the comments system in MT.
All system templates receive a weblog context that depends on their particular task.
- Template Modules
Template Modules are user-defined sub-templates that contain bits of template tags, text, and other markup that can be arbitrarily included in any other template. This allows you to keep all common code in one place so modifications can be made once from one place.
See "Reusing Template Markup with Modules" for more on Template Modules.
Templates Listing Screen
The Templates Listing screen displays a list of all weblog templates and modules, divided by type. You can reach this screen by clicking the Templates button on the Weblog Toolbar.
1 Tabs. Tabs for accessing template lists by type.
2 Create new (type) template link. Opens a New Template Screen for the given type. This link is not available for System templates.
3 Delete Action Button. Permanently removes any checked templates from the system.
4 Actions Pulldown Menu. A control for performing any additional actions on the checked templates.
5 Template Name. The name of the template, linked to its Edit Template screen.
6 Output File. The file name and additional path information where the output will be written. Index templates tab only.
7 Dynamic Flag. The template will generate output dynamically instead of statically. Index and archive templates only.
8 Linked Flag. The template is linked to a local file.
9 Rebuild w/ Indexes Flag. The template will be included in any batch rebuild of index templates. Index templates tab only.




Comments
"Template modules [...] can be arbitrarily included in any other template."
Not quite arbitrarily. They're processed as part of the template into which they're being included, so have to respect its context.
Posted by: Su
|
August 15, 2005 09:19 PM