Saturday, April 30, 2011

Conditional tags in blogger

  please read this before going down ----- how-to-put-widget-on-specific-page-of-your-blog

Conditional tags in blogger are used to tell browsers what to do in certain conditions. For example, when we use:

<b:if cond='data:blog.pageType == &quot;item&quot;'>
Hello world
<b:else/>
Goodbye Cruel World
</b:if>
It gives the browser the condition that when it’s an item (post) page then show “Hello world” otherwise, for every other pages show “Goodbye Cruel World”. Interesting, isn’t it.

A conditional tag comes handy when you are designing your Blogger blogs and customizing it from the scratch to take it to the next level. But first you need to know how many types of pages are there in Blogger and then how to use the conditional tags respective to that type of page. In this tutorial I will try to cover most of it, so let’s start.

Type of Pages in Blogger and Conditional Tags For It

1. Item Page
Item page refers to the individual post pages. It’s the page when you click the tile of the post or the read more link in blogger to read the full post. The conditional tag:
<b:if cond='data:blog.pageType == &quot;item&quot;'>
Hello World
</b:if>

2. Index Page
It refers to every other page except item (post) pages. The conditional tag:
<b:if cond='data:blog.pageType == &quot;index&quot;'>
Hello World
</b:if>

3. Archive Page
It’s for your archive pages in blogger. The tag will be:
<b:if cond='data:blog.pageType == &quot;archive&quot;'>
Hello World
</b:if>

4. Static Page
For static pages in blogger it will be:
<b:if cond='data:blog.pageType == &quot;static_page&quot;'>
Hello World
</b:if>

5. For a Particular Page
If you want to do something on a particular page then the conditional tag for it will be:
<b:if cond='data:blog.url == &quot;URL of the page&quot;'>
Hello World
</b:if>
The only thing that you have to do is replace “URL of the page” with your page’s URL which you want to have.

5. Homepage only
It's for the homepage only.
<b:if cond='data:blog.url == data:blog.homepageUrl'>
Hello World
</b:if>

Alternative Condition

Now suppose you want a conditional tag that alternates every rule. Say, in example 1, I showed how to display something only in post pages but just an “!” mark can alternate the condition. For example, when we apply:
<b:if cond='data:blog.pageType != &quot;item&quot;'>
Hello World
</b:if>
It will set the condition that to show it on every page other than post page.

Conclusion

“Hello world” is an example, but if it’s implemented correctly, you can add conditions to particular widgets/gadgets to show them in specific pages, which I will write in next tutorial. However, you can find in most of my tutorials that I have used it. 

No comments:

Post a Comment