If, else, and elseif Statements in blogger template

Published


Have you ever thought that editing blogger template is that easy if we know the basics of coding?
even if we have no such programming skills or just want to experiment,you will need to learn how "if else" statements works in blogger templates.

After reading this article, you will be able :

  • To Understand the basics of if else statements in blogger templates and widgets.

  • To give multiple condition to an if statement.

  • To Understand how blogger conditional tags works inside blogger's server.

  • To understand how easy it is to learn blogger's <b:if/> <b:else/> conditions.

But you should know that there is no method to call if else statements like in php or any other language just by calling something like this.

if (condition)
{
Condition is true - now Do something.
}
else
{
condition is not true - now Do something.
}


you know what are <b:if> <b:else> tags  in blogger templates ?
Yes, these are simply like if else statement.
You can use the b:if and b:else tags to display content in specific pages as per your conditions.
if you want to display your logo only on your homepage in blogger, then you can show it only on homepage by specifying the condition.

You can use the b:if and b:else tags to execute javascripts only where you wish to execute.
You can prevent executing javascript in a specific post or in homepage of your blog itself, just by passing an if else condition in your template.

Before further reading, please have a look at This post about blogger conditional tags, http://aslamise.blogspot.in/2012/12/all-blogger-conditional-tags.html that will help you to capture the syntax.

  • Blogger's if else condition's general syntax of use.


<b:if cond='condition'>
   [here goes the content to display if condition is true]
<b:else/>
   [here goes the content to display if condition is false]
</b:if>


For an example, if you want to display a text "Welcome to My Homepage!" on your homepage only,then the code goes like this.

<b:if cond='data:blog.url == data:blog.homepageUrl'>
<h1>Welcome to My Homepage!</h1>
</b:if>


If you are confused about what the above code does, i will explain to you

<b:if cond='data:blog.url == data:blog.homepageUrl'>
//This code lets blogger server to Check whether the current page url matchs homepage url.
//If i use it my blog, it will be executing like this <b:if cond='data:blog.url == "//aslamise.blogspot.com"'>
<h1>Welcome to My Homepage!</h1>
//Here goes the code to display if it is homepage.
</b:if>
//If condition Ends. Execution completed.


The <b:else/> tag is optional tag. Without a <b:else/> tag, the result will be either the content listed in the b:if section or simply nothing.

<b:if cond='data:blog.url == data:blog.homepageUrl'>
<h1>Welcome to My Homepage!</h1>
<b:else/>
<h1>This is not my homepage</h1>
</b:if>

So,You might have understood how to setup if or else conditions in blogger template,

Now, we should be knowing how to execute Multiple conditions in blogger template.
Multiple conditions means, you want to check whether all conditions are true, and not only one condition

<b:if cond='condition 1'>
<b:if cond='condition 2'>
[here goes the content to display if condition1 and then condition2 are true]
</b:if>
</b:if>


The <b:else/> tag is optional tag but The closing </b:if> is required. and if you dont close these conditional tags properly, blogger template will show errors.

Please Note that there are blogger conditional tags that we can use Globally ,Page Header,Blog Posts,Blog Archives,Profile,Text / HTML / JavaScript,Feed,Picture,Labels,List,Link List, or in Logo.

Some data tags are simply true/false values on their own, e.g. "allowComments" on a post.
With other pieces of data, you can compare them with specific values to get a true or false.
Here are some examples:

<b:if cond='data:post.showBacklinks'> True if the current post is set to show backlinks.(Apply only in blog posts)
<b:if cond='data:blog.pageType == "item"'> True if the current page is an item page (post page).
<b:if cond='data:displayname != "Aslam"'> True if this is not Aslam's display name.
<b:if cond='data:post.numComments > 2'> True if the current post has more than one comment.(Apply only in blog posts)
<b:if cond='data:post.numComments < 2'> True if the current post has less than one comment.(Apply only in blog posts)



Comments

Post a Comment