Some My Experiences

Header Ads

Sunday 16 December 2018

Control Flow in Blogger

This section describes the control flow in blogger. Control flow is used to alter the flow of statement execution with decision making and looping that will enabling execute particular blocks of code/show content conditionally.

Decision Making

b:if, b:elseif, & b:else Statement
We can use the b:if, b:elseif and b:else tags to execute particular statement in different cases.
Format:
<b:if cond='condition 1'>
  [SHOW CONTENT IF MEET `condition 1`]
<b:elseif cond='condition 2'/>
  [SHOW CONTENT IF MEET `condition 2` and NOT MEET `condition 1`]
<b:else/>
  [SHOW CONTENT IF `condition 1` and `condition 2` evaluates to false]
</b:if>
The b:elseif and b:else tags are optional. So we can use only just b:if, or b:if with b:elseif, or b:if with b:else as needed. For `condition`, we can put anything that will be evaluates to boolean value (true or false).
Example:
<b:if cond='data:post.allowComments'>
<b:if cond='data:blog.pageType != "static_page"'>
<b:if cond='data:blog.pageType == "static_page" AND data:blog.pageName == "About Us"'>
<b:if cond='data:post.labels any (l => l.name == "Tutorial")'>
<b:if cond='data:blog.pageType not in {"static_page", "index"}'>
<b:if cond='data:post.labels any (l => l.name in {"Technology", "Sport"})'>

b:switch statement
The use of b:switch statement is best in case when we need to show content based on same variable.
Format:
<b:switch var='[data]'>
  <b:case value="[value 1]" />
    [SHOW CONTENT IF data value is `value 1]
  <b:case value="[value 2]" />
   [SHOW CONTENT IF data value is `value 1]
  <b:default />
   [SHOW CONTENT IF data value is not equal to any case value above]
</b:switch>
Example:
<b:switch var='data:blog.pageType'>
  <b:case value="static_page" />
    <title>Static Page</title>
  <b:case value="item" />
    <title>Blog Post</title>
  <b:default />
    <h2>Other Page Type</h2>
</b:switch>

LOOPING

b:loop
The b:loop will execute statements or block code for each data of items individually.
Format:
<b:loop var='identifier' index='index' values='set-of-data'>
  [PUT CONTENT LOOP HERE]
</b:loop>
identifier will hold value of each item in loop. index attribut is optional, which is zero-based index. This index will save state current iteration number and is incremented.
Example:
<b:loop index='i' values='data:posts' var='post'>
  <b:if cond='data:i == 3'> 
    <div>Show ads when index number is 3</div>
  </b:if> 
  <div><data:post.title/></div>
</b:loop>
b:loop Allow us to loop between two number, in case we need loop with no set of data relation. Two number that define start and end can be increment (lower to higher) or decrement (higher to lower). example we loop from 1 to 3 that show div element with different class:
<b:loop var='number' values='1 to 3'>
  <div expr:id='"layoutId_" + data:number'></div>
</b:loop>

No comments:

Post a Comment