

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Liquid 添加自动化
<a name="sms-custom-templates-step2-automate"></a>

我们的自定义模板系统使用 [Liquid](https://shopify.github.io/liquid/) 进行自动化。它是开源内联标记语言。在 Liquid 中，单大括号之间的文本以及百分比符号是执行控制流或迭代等操作的指令或*标签*。双大括号之间的文本是一个变量或用于输出变量值的*对象*。

Liquid 最常见的用途是解析输入清单文件中的数据，并提取相关变量来创建任务。除非指定了注释前 Lambda，否则 Ground Truth 会自动生成任务。Ground Truth 或 [注释前 Lambda](sms-custom-templates-step3-lambda-requirements.md#sms-custom-templates-step3-prelambda) 返回的 `taskInput` 对象就是模板中的 `task.input` 对象。

输入清单中的属性将作为 `event.dataObject` 传递到模板中。

**Example 清单数据对象**  

```
{
  "source": "This is a sample text for classification",
  "labels": [ "angry" , "sad" , "happy" , "inconclusive" ],
  "header": "What emotion is the speaker feeling?"
}
```

**Example 使用变量的示例 HTML**  

```
<crowd-classifier 
  name='tweetFeeling'
  categories='{{ task.input.labels | to_json }}'
  header='{{ task.input.header }}' >
<classification-target>
  {{ task.input.source }}
</classification-target>
```

请注意，在上面的 `labels` 属性中添加了 ` | to_json`。这是一个筛选条件，用于将输入清单数组转换为数组的 JSON 表示形式。下一节将介绍变量筛选条件。

以下列表包括两种类型的 Liquid 标签，在自动化模板输入数据的处理时，这些标签可能会非常有用。如果您选择了以下标签类型之一，您将被重定向到 Liquid 文档。
+ [控制流](https://shopify.github.io/liquid/tags/control-flow/)：包括编程逻辑运算符，如 `if/else`、`unless` 和 `case/when`。
+ [迭代](https://shopify.github.io/liquid/tags/iteration/)：使您能够使用 for 循环之类的语句重复运行代码块。

  有关使用 Liquid 元素创建 for 循环的 HTML 模板的示例，请参阅中的.li [translation-review-and-correctionquid.htm](https://github.com/aws-samples/amazon-sagemaker-ground-truth-task-uis/blob/8ae02533ea5a91087561b1daecd0bc22a37ca393/text/translation-review-and-correction.liquid.html) l。 GitHub

有关更多信息和文档，请访问 [Liquid 主页](https://shopify.github.io/liquid/)。

## 变量筛选条件
<a name="sms-custom-templates-step2-automate-filters"></a>

除了标准的 [Liquid 筛选条件](https://shopify.github.io/liquid/filters/abs/)和操作之外，Ground Truth 还提供了几个额外的筛选条件。通过在变量名称之后放置竖线 (`|`) 字符，然后指定筛选条件名称，以应用筛选条件。筛选条件可以采用以下形式链接起来：

**Example**  

```
{{ <content> | <filter> | <filter> }}
```

### 自动转义和显式转义
<a name="sms-custom-templates-step2-automate-filters-autoescape"></a>

默认情况下，输入将进行 HTML 转义，以防止在变量文本和 HTML 之间产生混淆。您可以显式添加 `escape` 筛选条件，以使其对于正读取正在进行转义的模板源的用户更显而易见。

### escape\$1once
<a name="sms-custom-templates-step2-automate-escapeonce"></a>

`escape_once` 可确保您的代码已经转义，而不会再次重新转义。例如，确保 &amp; 不会变为 &amp;amp;。

### skip\$1autoescape
<a name="sms-custom-templates-step2-automate-skipautoescape"></a>

当您的内容要用作 HTML 时，`skip_autoescape` 很有用。例如，您可能在边界框的完整说明中有一些文本段落和一些图像。

**请谨慎使用 `skip_autoescape`**  
模板中的最佳实践是避免使用 `skip_autoescape` 传递功能代码或标记，除非您绝对确信您对正传递的内容具有严格的控制。如果您传递用户输入，就可能使您的工作人员遭受跨站点脚本攻击。

### to\$1json
<a name="sms-custom-templates-step2-automate-tojson"></a>

`to_json`会将你输入的内容编码为 JSON（JavaScript 对象表示法）。如果您向其提供对象，它会对该对象序列化。

### grant\$1read\$1access
<a name="sms-custom-templates-step2-automate-grantreadaccess"></a>

`grant_read_access` 获取 S3 URI 并将其编码为 HTTPS URL（具有针对该资源的短期访问令牌）。这样，就可以向工作人员显示存储在 S3 存储桶中但原本无法公开访问的照片、音频或视频对象。

### s3\$1presign
<a name="sms-custom-templates-step2-automate-s3"></a>

 `s3_presign` 筛选器的工作方式与 `grant_read_access` 筛选器相同，`s3_presign` 获取 Amazon S3 网址并将其编码为 HTTPS 网址（具有针对此资源的短期访问令牌）。这样，就可以向工作人员显示存储在 S3 存储桶中但原本无法公开访问的照片、音频或视频对象。

**Example 变量筛选器**  
Input  

```
auto-escape: {{ "Have you read 'James & the Giant Peach'?" }}
explicit escape: {{ "Have you read 'James & the Giant Peach'?" | escape }}
explicit escape_once: {{ "Have you read 'James &amp; the Giant Peach'?" | escape_once }}
skip_autoescape: {{ "Have you read 'James & the Giant Peach'?" | skip_autoescape }}
to_json: {{ jsObject | to_json }}                
grant_read_access: {{ "s3://amzn-s3-demo-bucket/myphoto.png" | grant_read_access }}
s3_presign: {{ "s3://amzn-s3-demo-bucket/myphoto.png" | s3_presign }}
```

**Example**  
Output  

```
auto-escape: Have you read &#39;James &amp; the Giant Peach&#39;?
explicit escape: Have you read &#39;James &amp; the Giant Peach&#39;?
explicit escape_once: Have you read &#39;James &amp; the Giant Peach&#39;?
skip_autoescape: Have you read 'James & the Giant Peach'?
to_json: { "point_number": 8, "coords": [ 59, 76 ] }
grant_read_access: https://s3.amazonaws.com/amzn-s3-demo-bucket/myphoto.png?<access token and other params>
s3_presign: https://s3.amazonaws.com/amzn-s3-demo-bucket/myphoto.png?<access token and other params>
```

**Example 自动化的分类模板。**  
要自动执行简单文本分类示例，请将推文文本替换为变量。  
文本分类模板如下，添加了自动化功能。 changes/additions 它们以粗体突出显示。  
前面示例中的推文文本现已替换为一个对象。`entry.taskInput` 对象使用 `source`（或者在注释前 Lambda 中指定的其他名称）作为文本的属性名称，并通过双大括号将其直接插入 HTML 中。