About

Labels

slider

Recent

Navigation

What are exactly Blogger's Lambda expressions?

Javascript was never meant to be the first choice to change DOM on load, instead Blogger layout data tags helps us to make the template with native codes, so that the response time gets better and lesser Javascript is used. Previously added expression were indeed useful ones and the new expressions are much more powerful.

Loops are basic concept in Javascript as well as in Blogger. Just like we loop the array and access each item (using <b:loop> tag), we can do the same with Blogger. But lambda expressions let us do processing with array without looping it.
 Lambda expressions again works on the basic principle of if and else condition. Point to note that these new expressions work on a set of elements and practically Blogger have 3 sets only we can work on.
  • Comments
  • Labels
  • Posts

What lambda expression does?

These have the same function as filter, map and if...else loop does in Javascript. They take in an array and return a new sorted array. They are fast and native and importantly you have many options like you can get a returned array of any element matches, all element matches or none of the element matches the given condition. I found this one on the forum.
<b:if cond=’data:post.labels any (label=> label.name == “Flower”)’>
<img src=’/img/flower.jpg’ />
</b:if>
Above code check each post and each label in it. When a label name flower is found, an image tag is added.

Basic syntax of lambda expression is:
[set of items] [lambda operator] ([lambda expression])

The syntax for lambda expression is:
[variable name] => [expression]

Set of items

Currently there are only three sets I know that are: posts, labels and comments. If there are other than these, hit them in comments.

Lambda Operator

Lambda operators are the keywords which determine the way items have to be returned. Here is the list of them:
  • any
  • all
  • none
  • count
  • filter
  • map
  • first
Example of each of them is further explained in this post.

Lambda expression

Lambda expression is the condition which will return true or false for specific item that goes into the condition. Examples will clear what lambda expressions are.

Working Examples

Each lambda operator takes input and process them in different ways. All the examples below are working under blog post widget and it is where we actually access the post array and as I mentioned above, lambda expressions work outside the loop. Let us dive into examples of each lambda operator.

any

It returns true if any one the item in the set matches the lambda expression. This one helps us to do some experiments with labels, just like I shared one above. If you are on home page or index pages then you can identify if any of the post is not published by you then you can identify them.
Here is what we can do with `any` lambda operator.
<b:if cond='data:blog.pageType == "index"'>
<b:if cond='data:posts any (post => post.author != "Hamza Dhamiya")'>
<!--Do some work here-->
</b:if>
</b:if>
A point to note here that you will be able to filter the post which are present on that page, means you cannot access your all posts from a single page.

all

This lambda operator returns true if all items in array matches the lambda expression. In below example we are going under post loop and accessing the labels array. We are using `in` keyword which is described here.
  <b:if cond='data:post.labels all (label => label.name in {"HTML","CSS","Javascript"})'>
<!--Do some work here-->
</b:if>
Above code let you see if the post have labels of the choice only, if it doesn't then it will return false; else if all labels are matching the condtion then it will return true.

none

It is same as above one, just difference is that it returns true only if none of the items in array matches the lambda expression.

count

This lambda operator returns the number of items of an array which returns true in lambda expression. By taking the above example
  <b:if cond='data:post.labels count (label => label.name in {"HTML","CSS","Javascript"})'>
<!--Do some work here-->
</b:if>
Now the code above will return number of labels which are either HTML, CSS or Javascript; means maximum number of labels which could return true are 3.

filter

This one is quite different than above ones. Filter operator returns an array. Therefore we need loop to make it work. This operator basically takes in an array and returns an array containing those elements which returns true for lambda expression. Here is an example of post having author named as "Hamza Dhamiya", it means you can filter posts according to author.
<b:loop var='post' values='data:posts filter (post => post.author == "Hamza Dhamiya")'>
<b:include data='post' name='post'/>
</b:loop>

map

Map operator takes in the set and returns the result of lambda expression. Here is a basic example of it.
<b:loop values='data:post.labels map (label => "#" + label.name)' var='label'>
<a expr:href='data:label.url'><data:label/></a>
</b:loop>
In result all the labels will be returned with "#" attached to them.

first

It is same as filter but returns just the first item which returns true for lambda expression.

Conclusion

This was a basic walk through of newly launched lambda expression. I will try to put some more productive uses related to these.
Share
Banner

Muhammad Hamza

Themelet provides the best in market today. We work hard to make the clean, modern and SEO friendly blogger templates.

Post A Comment:

0 comments: