如何使用Google Blogger Lambda运算符 [英] How to use Google Blogger Lambda Operators

查看:98
本文介绍了如何使用Google Blogger Lambda运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Blogger为其模板语言实现了一组新的Lambda表达式运算符.请参阅: https://blogger.googleblog.com/2016/05/more-custom-template-flexibility.html https://productforums.google.com/forum/#!topic/blogger/l3phi8bscGY .

Google Blogger has implemented a new set of Lambda expression operators for its template language. See: https://blogger.googleblog.com/2016/05/more-custom-template-flexibility.html and https://productforums.google.com/forum/#!topic/blogger/l3phi8bscGY .

给出的示例代码(对b:if/进行了修改)是:

The example code given (the b:if/ amended) is:

<!-- Show a Flower image if the post has the label flower -->
<b:if cond='data:post.labels any (l => l.name == "Flower")'>
  <img src=’/img/flower.jpg’ />
</b:if>

我不知道如何在模板中使用它.有人可以提供一些有效的代码,我可以将其放入模板中并查看其工作情况.

I can't figure out how to get this to work in a template. Would someone please provide some working code I could drop in to a template and see it working.

更新: 这似乎有效.但是,如何删除循环?

UPDATE: This seems to work. But how, or can I, remove the loop?

<b:section class='Test1' id='Test1' maxwidgets='' showaddelement='no'>
    <b:widget id='Blog2' locked='true' title='Blog Posts' type='Blog' version='1' visible='true'>
        <b:includable id='main'>

            <b:loop values='data:posts' var='post'>
                <h1>Post found</h1>

                <b:if cond='data:post.labels any (label  => label.name == "flower")'>
                    <h1>Flower!</h1>
                </b:if>

            </b:loop>
        </b:includable>
    </b:widget>
</b:section>

例如,如果我删除循环,然后将其替换为:

For example, if I remove the loop, and replace it with :

<b:if cond='data:posts any (p  => p.title  != "bob")'>                  
    <h1>Post found</h1>           
</b:if>>

当我有几篇帖子都没有标题为bob时,仅找到一篇帖子!我尝试将lambda运算符从任何切换为不更改而进行过滤.

Only one post is found, when I have several posts none of which are titled bob! I have tried switching the lambda operator from any to filter with no change.

推荐答案

我的问题的解决方案

对我来说很愚蠢,如果我希望能够使用循环,则需要指定一个循环:

Stupid of me, if I expect to be able to use a loop I need to specify a loop:

<b:loop values='data:posts filter (p  => p.id  != 0)'  var='post'>                    
  <h1>Post found: <data:post.title/></h1>           
</b:loop>

如何使用Blogger Lambda运算符

对于那些来这里寻求帮助的人,我建议以下文章,其中介绍了如何全面使用Lambda表达式: https://productforums.google.com/forum/#!topic/blogger/l3phi8bscGY

For those coming here trying to find help I recommend the following article which explains how to use Lambda expressions quite comprehensively: http://www.bloggerever.com/2016/05/what-are-exactly-bloggers-lambda.html and is based on https://productforums.google.com/forum/#!topic/blogger/l3phi8bscGY.

本质上有7个Lambda运算符

In essence there are 7 Lambda operators

  • 任何
  • 全部
  • 无人
  • 计数
  • 过滤器
  • 地图
  • 第一

每个都可以在if或循环条件语句中使用.以下一些未经测试的示例代码可帮助您入门.

Each of which can be used either inside an if or loop conditional statement. Some untested sample code follows to help get you started.

如果Lambda中的任何项目返回true,则 any 运算符将返回true,因此,例如,如果帖子具有标签labela或labelb与之关联,则以下代码将返回true:

The any operator returns true if any items in the Lambda return true, so for example, the following code would return true if a post had a label labela or labelb associated with it:

  <b:if cond='data:post.labels any
               (l  => l.name in {"labela","labelb"})'>
      ...Code here...
</b:if>

如果

全部同时具有labela和labelb,则将返回true:

all would return true if it had both labela and labelb associated with it:

  <b:if cond='data:post.labels all  
               (l  => l.name not in {"labela","labelb"})'>
      ...Code here...
</b:if>

如果

没有都没有关联的labela和labelb,则将返回true:

none would return true if it had neither labela and labelb associated with it:

  <b:if cond='data:post.labels all  
               (l  => l.name not in {"labela","labelb"})'>
      ...Code here...
</b:if>

计数将返回0、1或2,具体取决于帖子既没有与其关联的labela或labelb,也没有只是具有labela或既与labela又与labelb关联的

count would return 0, 1 or 2 depending on whether a post had neither labela or labelb associated with it, or just labela or both labela and labelb

  <b:if cond='data:post.labels count
               (l  => l.name not in {"labela","labelb"})'>
      ...Code here...
</b:if>

过滤器将返回一个数组,并需要一个循环.下面的示例将打印出每个帖子的标题,除非它的id为0(这是我不可能的!)

filter will return an array and requires a loop. The example below would print out the title of each post unless it had an id of 0 (which i don't is possible!)

<b:loop values='data:posts filter (p  => p.id  != 0)'  var='post'>                    
    <h1>Post found: <data:post.title/></h1>           
</b:loop>

第一(类似于过滤器),但只会返回第一个匹配项.

first like filter, but will return the first match only.

<b:loop values='data:posts first(p  => p.timestamp  == "4.2.09")'  var='post'>                    
    <h1>Post found: <data:post.title/></h1>           
</b:loop>

地图返回一个数组集,其中包含Lambda的每个结果.下面的代码将在h1标签中显示带有>>的标签.

map returns an array set containing each result from the Lambda. The code below would display labels in a h1 tag with >> prepended.

<b:loop values='data:post.labels map (l=> ">>" + l.name)' var='label'>
     <h1><data:label/></h1>
</b:loop>

最后的提示:链接到该文章的文章建议Lambda只能应用于发布,标签和评论元素.它当然可以与前两个一起使用,但是我还没有用注释尝试过.

A final note: the article linked to suggests the Lambda's may only be applied to post, label and comment elements. It certainly works with the first two but I haven't tried it with comments.

这篇关于如何使用Google Blogger Lambda运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆