For The Record: This is the first unofficial detailed tutorial of its kind across the web on Blogspot layout tags
Must Read: How to Show and Hide Widgets in Blogspot?
List of Operators Supported by Blogger Templates
At present blogspot templates support all major operators used in basic programming which are:
Operator | Syntax | Definition |
---|---|---|
OR | or and || | OR or UNION is used to combine several conditions, any of which if true would render the HTML content. You can declare it either as or or||. Prevents repeating b:iftags |
AND | and and && | And or INTERSECTION is used to combine two or more than two conditions. All conditionals must be true. You can use eitherand or the ampersand symbols &&. Prevents repeating b:iftags |
NOT |
| NOT or EXCLUSION is used when you want to exclude something from the condition. Use eithernot or ! Best used withMembership operator |
Equivalence | == and != | Equity is used to to check what a data value is. Here '==' means equal to and '!=' means not equal to |
Ternary Selector | [condition] ? [result when true] : [result when false] | Used to select one of two options, based on a condition. |
Membership | in and contains | Used to check whether a value is one of several possible values. You can define the set using either square brackets [ ] or Curly braces { }. Prevents repeating OR |
Example Usage of These Operators
Below I will give brief examples of how smartly these operators simplify an expression and prevents repeating a condition.
OR Example
Suppose you wish to show a HTML code only inside index pages (search pages, homepage) and item pages (Posts) but not on Static pages or archive pages. This is how you will write down the conditional expressions using widget layout tags.
Previously without 'or' the expression was written as such:
Now with 'or' operator the expression is simplified as such:
or you can use || instead of or:
AND and Equivalence Example
Suppose you wish to show a HTML code only on Search pages but not on homepage. Here we are selecting label pages only out of index pages.
Previously without 'and' the expression was written as such:
Now with 'and' operator the expression is simplified as such:
or you can use && like this
You might be thinking what is the different between ! and != then lets try the next example to clear this confusion.
NOT and Membership Example
Unlike != which is only used when comparing/equating two tags, not (!) is used to negate an entire expression. not is mostly commonly used with Membership operator as shown below.
Lets suppose we wish to show a HTML code on all pages except the Posts and Static Pages. We will format our expression as shown below.
Previously without membership operator (contains or in) and NOT operator the expression was written as such:
Now with NOT and membership operator the expression is simplified as such:
you can also write it like this by placing not before the expression. Note that you can use either [] or {}, same meaning for membership operators.
Or like this using contains:
Combined Use of OR and AND
You can surely make your expression even more in-depth by using OR and AND operator together in a single expression.
In the example below, the HTML code will render only when the page visited by user is either the archive page or Search/Label Page.
You can group multiple conditions inside the round braces and then compare them as against others. As simple as that!
Ternary Selector Example
Ternary selector is most useful when you need to apply one class out of two classes to any HTML DOM.
In the example below we will use a ternary operator in a div tag to apply a class comments-open when comments are allowed and apply the class comments-closed when the page no more accepts comments
This is an excellent way to show your readers a special message when you are no more accepting comments for certain posts.
0 comments:
Post a Comment