11 Aprili 2003

Container placeholders

Some Movable Type placeholders go anywhere, any time. Your <MTBlogEntryCount> will be the same no matter where you plunk it down on your page; it is governed by how much you post, not where you stick the information about how much you post.

These placeholders are relatively easy to understand and deal with, being (vis-a-vis the template) context-free. Unfortunately, not all placeholders are like that. Those that aren’t fall into three types:

  1. Some placeholders establish a context, within which other placeholders do certain things. The Movable Type documentation calls these “container tags,” but I am going to continue my convention of calling MT-specific constructs “placeholders,” reserving the word “tag” for HTML constructs. These, then, are “container placeholders.”
  2. Some placeholders are specifically designed to work only within the context of a specific container placeholder. Call them “contained” placeholders, for the sake of argument.
  3. Still other placeholders change their behavior based on the context established by container placeholders. I’ll call these “context-dependent placeholders.”

A quick example, just for (say it with me) context: The <MTEntryTitle> placeholder stands for the title of a given blog entry. It is contained, only usable within <MTEntries>, which is a container placeholder standing for generic blog entries. When you think about it, of course, this makes sense: an entry title has no meaning outside blog entries.

Container placeholders, just for your information, get used for two kinds of blog parts: parts that repeat with different data, and parts that may or may not be there. Blog entries, for example, repeat: a single blog page may (and usually does) contain multiple entries, all with the same structure and markup despite differing content.

Comment-related text or links may or may not be there: if, for example, some of your entries allow comments and others don’t, you may want to show a comments link if you’re allowing comments but suppress the link if you’re not. You can do that, but you have to put the template code for the comments link inside a container that tells MT “if I allow comments to this post, do this; otherwise, not.”

So how do you know if a given placeholder you’re looking at is a container? Look for an end-placeholder, which will have a slash after the < just as an end-tag in HTML does. If you see <MTEntries>, somewhere a little later you will also see </MTEntries>. Container placeholders must have end-tags; if you do not close them off, your template will not work properly and Movable Type will hand you an obscure error when you try to rebuild.

A cheat specifically for containers representing repeated parts of a page: is the placeholder name plural? <MTEntries> is a container; <MTEntryTitle> is not. This isn’t universal; <MTArchiveList> is a container of this type, for example. Still, worth checking.

A cheat specifically for containers representing optional parts of a page: look for the word “If” in the placeholder name. Optional code for comments, for example, would be placed inside the container <MTEntryIfAllowComments> </MTEntryIfAllowComments>.

If you still aren’t sure, look for the placeholder in the TEMPLATE TAGS section of the MT help files; containers are clearly stated to be such.

Movable Type container placeholders generally have contained placeholders, as I mentioned: placeholders that go inside one specific container and will not work anywhere else. If you put <MTEntryTitle> outside <MTEntries>, for example, your blog will not rebuild properly; the error you get should tell you which placeholder has been orphaned in your template. The fix? Add the container to the template, or check that the container is closed off in the right place, after the contained placeholder that isn’t working right.

If you know the name of the container placeholder, its contained placeholders generally aren’t hard to spot. Remember how most container placeholders are plural? The names of their contained placeholders almost always begin with the singular. You can be dead sure that whatever <MTEntryFoo> is (I just made it up; it isn’t a real placeholder, so don’t use it!), it is a contained placeholder within <MTEntries>.

Just when you thought this couldn’t get any more complicated… some placeholders behave a little differently depending on the container they’re in. Context-dependent placeholders, remember them? They will be the topic of the next post. Before I’m done, I’ll make you a list of container placeholders and meaning-shifting placeholders, just for reference.