Senin, 15 Agustus 2016

Types of Operators

This section describes the operators you can use inside an expression on a Widget tag for Layouts.

Equivalence

When to use an 'equivalence' operator

The equivalence operators are useful when you want to check what a data value is.


Supported Syntax

== and !=


Examples
Not-equal operator
Use the not-equal != operator to check if the title of a widget is empty.

<b:if cond=’data:title != “”’>

   <h3> <data:title /> </h3>

</b:if>
Equal operator
Use the == operator to check if you're on the home page.

<b:if cond=’data:blog.pageType == “index”’>

  <p>Homepage! </p>

</b:if>

And

When to use the 'and' Operator

The and operator is most useful when you want to combine the conditions under which a Widget tag is evaluated, such as a b:if tag. This prevents you having to nest multiple b:if tags inside each other.


Supported Syntax

and and &&


Examples

Here's an example that shows when to use the and operator in a b:if tag.
Without 'and'
Without an and operator, we would need two nested b:if tags.


<b:if cond=’data:blog.pageType not in {"item","static_page"}’>
   <b:if cond=’data:post.allowComments’>
       <b:include name='comment_count_picker' data='post' />
  </b:if>
</b:if>


With 'and'
With an and operator, we can put both conditions into one b:if tag.
<b:if cond=’data:blog.pageType not in {"item","static_page"} and data:post.allowComments’>
    <b:include name='comment_count_picker' data='post' />

</b:if>

Or

When to use the 'or' operator

The or operator is most useful when you want to combine several conditions, any of which would be satisfactory, under which a Widget tag such as a b:if tag is rendered. This prevents you having to repeat yourself.

Supported Syntax

or and ||

Examples

Here's an example that shows when to use the or operator in a b:if tag.
Without 'or'
With an or operator, we would need two b:if tags.

<b:if cond='data:imagePlacement == "REPLACE"’>
     <b:include name="image" />
</b:if>
<b:if cond=’data:imagePlacement == “BEFORE_DESCRIPTION”’>
     <b:include name=”image” />
</b:if>

With 'or'
With an or operator, we can put both conditions into one b:if tag.
<b:if cond=’data:imagePlacement == “BEFORE_DESCRIPTION” or data:imagePlacement == “REPLACE”’>
     <b:include name=”image” />
</b:if>

Not

When to use the 'not' operator

The not operator is most useful when you want to operate under the condition that something is not true, like ab:if tag. This prevents you having to write an empty b:if tag just so that you can populate the b:else tag.

Supported Syntax

! and not

Examples

Here's an example that shows how to use the not operator in a b:if tag, to only show a byline on some pages.

<b:if cond=’not data:blog.pageType in {“static_page”, “index”}’>

    <b:include name=”comments_byline” />

</b:if>


Ternary

When to use a Ternary operator

You can use the ternary operator to select one of two values in-line, such as on an attribute of an HTML tag.

Syntax

[condition] ? [result when true] : [result when false]

Example

Here's an example that shows how to use the ternary operator in an HTML div tag to apply a different CSS class when comments are allowed, then when they're not allowed.

<div expr:class=’data:post.allowComments ? “comments” : “no-comments”’>


</div>

Membership

When to use a Membership operator

You can use the membership operator to check if a value is one of several possible values. This prevents you having to have several or conditions with equivalence operators.

Syntax

Define a set using { } or [ ], then apply the in and contains operators.

Examples

Here's an example that shows how to use the contains membership operator to check if the page type is a static page, or a single post.

<b:if cond=’{“item”, “static_page”} contains data:blog.pageType’>

  <p>Single Post or Page! </p>

</div>

Here's an example that shows how to use the in membership operator to check if the page type is the home page, or an archive page.

<b:if cond=’data:blog.pageType in [“index”, “archive”]’>

  <p>Homepage or Archive Page! </p>


</div>

Lambda

When to use a Lambda expression

Lambda expressions are useful when you want to check if a condition is true for one, all, or none of the items in a set. You can also use it to filter a set of items, or map a set of items to a new set.

Syntax

[set of items] [lambda operator] ([lambda expression])
Lambda expressions have the format
[variable name] => [expression]
Note that variable name can be any string of letters. Generally, for brevity, we choose a relevant letter, like x, but it could be a longer name like label or myVariableName.
As an example, an expression for determining the count of posts with at least one comment would be:
data:posts count (p => p.numComments > 0)
In this example, data:posts is the set, count is the lambda operator, and p => p.numComments > 0 is the lambda expression. The count operator counts the number of items in the set data:posts for which the expression p => p.numComments > 0 is true, i.e. the number of posts with at least one comment.
As another example, to add a flower image if the post is labelled ‘Flower’:
<b:if cond=’data:post.labels any (label => label.name == “Flower”)’>
   <img src=’/img/flower.jpg’ />
</b:if>

Lambda Operators


any
Returns true if any of the items in the set returns true for the lambda expression
all
Returns true if all of the items in the set return true for the lambda expression
none
Returns true if none of the items in the set return true for the lambda expression
count
Returns the count of the items in the set which return true for the lambda expression
filter
Returns a set of items for which the lambda expression is true
map
Returns a set containing each of the results of the lambda expression
first
Returns the first item for which the lambda expression is true

Resize

When to use image resize operators

The image resize operators are useful when you want to ensure that an image is displayed in a suitable size. Generally this will be used to get a cropped thumbnail of an image, or to create a responsive image for a mobile-friendly template.

Syntax

Resize a single image url
resizeImage(imageUrl, newSize, optionalRatio)
The resizeImage operator takes 3 parameters:
  1. imageUrl - The original URL of the resizable image.
  2. newSize - The new width of the image
  3. (optional) ratio - The integer ratio of width to height for the resized image, e.g. “1:1” or “4:3”
Notes
  • If the imageUrl parameter is not a resizable image, the resizeImage function will return the original imageUrl.
  • The ratio must be integer numbers.
  • If the ratio is provided, the image will be cropped to those exact dimensions.
Example
<img expr:src=’resizeImage(data:post.firstImageUrl, 300)’ />

Create a responsive image srcset

sourceSet(imageUrl, newSizes, optionalRatio)
The sourceSet operator takes 3 parameters:
  1. imageUrl - The original URL of the resizable image.
  2. newSizes - An array of new widths of the image
  3. (optional) ratio - The integer ratio of width to height for the resized image, e.g. “1:1” or “4:3”
Notes
  • If the imageUrl parameter is not a resizable image, the sourceSet function will return an empty string
  • The ratio must be integer numbers
  • If the ratio is provided, the image will be cropped to those exact dimensions
Example

<img expr:src=’data:post.firstImageUrl’ expr:srcset=’sourceSet(data:post.firstImageUrl, [128, 256, 512])’ />

Sumber : https://productforums.google.com/forum/#!topic/blogger/59LGf47pPP8

Tidak ada komentar:

Posting Komentar