Monday, May 28, 2007

Leveraging Conditional Tags in Blogger Templates

Blogger Style:
Working with:
If you have worked with Blogger templates for any length of time, then you are aware of what a Tag is and how it can insert information into your Blog at the exact location where the Tag appears. There are Tags for just about everything you can think of -- but what if you do not want certain types of information to appear on your Post Item page or an Archive page?

There are a second set of Blogger template tags known as Conditional Tags which can allow you to specify what information to display under certain conditions. The Conditional Tags are dependant upon the page type you are viewing -- those types are the Main Page, Archive Page and Item Page. A fourth Conditional Tag is an "OR" combination of the Main/Archive pages.
... show this on Main page only

... show this on Archive page only

... show this on Item page only

... show this on Main or Archive page only

You can use any Conditional Tag more than once in your template. Mix and match them wherever you want to.

Practical Uses for Conditional Tags

Let's say that you want to display a short introduction of what your blog is about on your Main Page before your Posts start to be displayed.
<h1>The Widget Factory Blog</h1>
<p>Hello and welcome to the Widget Factory,
a world of widget news, reviews and fun!</p>

The introduction will not appear on any of your Item or Archive pages.

If you are listing your Recent Posts in the sidebar of your blog, then having them on your Main Page is a little redundant. Also, the list is of no real value on your Archive pages. Let's display the list on your Item Pages only.
<h3>Latest Entries</h3>
<li><a href="<$BlogItemPermalinkURL$>">

Conditional Tags work in the <head> section as well. For example, take this following code snippet which alters the your Page Title.

Normally an Item Page will have a Title in the form of "Site Name - Post Title" which comes from the Template Tag <$BlogPageTitle$>. We can workaround this by using Conditional Tags.
In the <head> section, find this:

Replace with this:
<title>Your Sitename - Site Description</title>

<$BlogItemTitle$> - Your Sitename</title>

As you can see, there are many uses for Conditional Tags. Try using this method for displaying your Blogroll on your Main Page only or putting a short description of your blog in the Item Pages only. You are only limited by your imagination.