Aug 18, 2010

f Comment

How To Tell The Page Type In Google Blogger's Template Language?

Amazon In Google Blogger's template HTML I'd like to be able to tell what page type the current page is so I can treat it accordingly. The template is constructed with Blogger's own syntax, tags and attributes. What's the syntax and what does each special tag mean? Read on to find answers to all these questions in MINUTES!

Solution
First of all here's a screen shot of the Blogger template HTML editor user interface:

Blogger Template HTML Editor Window

The text area is where you edit the template HTML so that your blog displays what you want it to display. All pages on your blog share this template, so if you want to treat pages differently according to their page type you need to create such conditional statements. For example you may want the homepage to contain a pitch line that you don't want other pages to contain; you may want label pages to simply list links to the corresponding posts without showing the actual post bodies; etc.

Here's the deal: How do you know whether the current page is the homepage, a single post, a static page, an archive page, or a label page? What condition do I need to test? You need to be able to make this distinction to treat each type of page differently. Surprisingly the Blogger documents and references regarding widget tags for layouts and page elements tags for layouts do NOT address this question thoroughly. They merely say

data:blog.pageType specifies the type of the current page. One of 'item', 'archive', or 'index'.
And it says nothing about what each value means. How frustrating Some values are obvious but others aren't. After some digging I figured out the following (value:meaning):

index : homepage such as https://one-minute-info.blogspot.com/ or label page such as https://one-minute-info.blogspot.com/search/label/Joomla
item : item page such as https://one-minute-info.blogspot.com/2010/08/target-page-type-in-blogger-template.html
static_page : static page such as https://one-minute-info.blogspot.com/p/about.html
archive : archive page such as https://one-minute-info.blogspot.com/2011_07_01_archive.html

To tell homepage and a label page apart test whether data:blog.url equals data:blog.homepageUrl. If so then this is the homepage. Otherwise use data:blog.pageType. Here's the complete code that has a condition block for each page type:
<b:if cond='data:blog.url == data:blog.homepageUrl'>
<!-- homepage -->
<b:else/>
  <b:if cond='data:blog.pageType == &quot;index&quot;'>
  <!-- label page -->
  <b:else/>
    <b:if cond='data:blog.pageType == &quot;item&quot;'>
    <!-- single blog post -->
    <b:else/>
      <b:if cond='data:blog.pageType == &quot;static_page&quot;'>
      <!-- static page -->
      <b:else/>
        <b:if cond='data:blog.pageType == &quot;archive&quot;'>
          <!-- archive page -->
          <b:else/>
            <!-- unknown page; shouldn't be possible -->
          </b:if>
      </b:if>
    </b:if>
  </b:if>
</b:if>
Now you should be able to distinguish among the page types of your blog. Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael