# 内置助手

# #if

你可以使用 if 助手有条件地渲染块。 如果其参数返回 falseundefinednull""0[],Handlebars 将不会渲染该块。

template
<div class="entry">
{{#if author}}
<h1>{{firstName}} {{lastName}}</h1>
{{/if}}
</div>

当你将以下输入传递给上面的模板时

input
{
  author: true,
  firstName: "Yehuda",
  lastName: "Katz",
}

这将产生如下结果:

output
<div class="entry">
<h1>Yehuda Katz</h1>
</div>

如果输入是空的 JSONObject {},则 author 将变为 undefined,并且 if 条件失败,导致输出如下:

<div class="entry"></div>

使用块表达式时,你可以指定在表达式返回假值时要运行的模板部分。 标有 else 的部分称为 "其他部分"。

template
<div class="entry">
{{#if author}}
<h1>{{firstName}} {{lastName}}</h1>
{{else}}
<h1>Unknown Author</h1>
{{/if}}
</div>

# includeZero

可以设置 includeZero=true 选项以将条件视为非空。 这有效地确定了 0 是由正路径还是负路径处理。

{{#if 0 includeZero=true}}
<h1>Does render</h1>
{{/if}}

# Sub-Expressions

助手是向模板添加自定义逻辑的建议方法。 你可以编写任何辅助程序并在子表达式中使用它。

例如,在检查变量的初始化时,内置 #if 检查可能不合适,因为它对于空集合返回 false(请参阅 Utils.isEmpty)。

你可以编写一个检查 "undefined" 的辅助程序,例如:

preparationScript
Handlebars.registerHelper('isdefined', function (value) {
  return value !== undefined;
});

然后使用你的助手作为子表达式:

template
{{#if (isdefined value1)}}true{{else}}false{{/if}}
{{#if (isdefined value2)}}true{{else}}false{{/if}}

# #unless

你可以使用 unless 辅助函数作为 if 辅助函数的逆函数。 如果表达式返回一个假值,它的块将被渲染。

template
<div class="entry">
{{#unless license}}
<h3 class="warning">WARNING: This entry does not have a license!</h3>
{{/unless}}
</div>

如果在当前上下文中查找 license 返回一个假值,Handlebars 将渲染警告。 否则,它不会渲染任何内容。

# #each

你可以使用内置的 each 辅助程序迭代列表。 在块内,你可以使用 this 来引用正在迭代的元素。

template
<ul class="people_list">
  {{#each people}}
    <li>{{this}}</li>
  {{/each}}
</ul>

当与此上下文一起使用时:

input
{
  people: [
    "Yehuda Katz",
    "Alan Johnson",
    "Charles Jolley",
  ],
}

将导致:

output
<ul class="people_list">
    <li>Yehuda Katz</li>
    <li>Alan Johnson</li>
    <li>Charles Jolley</li>
</ul>

你可以在任何上下文中使用 this 表达式来引用当前上下文。

你可以选择提供 else 部分,该部分仅在列表为空时显示。

template
{{#each paragraphs}}
<p>{{this}}</p>
{{else}}
<p class="empty">No content</p>
{{/each}}

当循环 each 中的项目时,你可以选择通过 {{@index}} 引用当前循环索引。

{{#each array}} {{@index}}: {{this}} {{/each}}

此外,对于对象迭代,{{@key}} 引用当前键名称:

{{#each object}} {{@key}}: {{this}} {{/each}}

迭代数组时,迭代的第一步和最后一步通过 @first@last 变量来记录。

嵌套的 each 块可以通过基于深度的路径访问迭代变量。 例如,要访问父索引,可以使用 {{@../index}}

# #with

with 辅助程序允许你更改模板部分的评估上下文。

template
{{#with person}}
{{firstname}} {{lastname}}
{{/with}}

当与此上下文一起使用时:

input
{
  person: {
    firstname: "Yehuda",
    lastname: "Katz",
  },
}

将导致:

output
Yehuda Katz

with 还可以与块参数一起使用来定义当前块中的已知引用。 上面的例子可以转换为

template
{{#with city as | city |}}
  {{#with city.location as | loc |}}
    {{city.name}}: {{loc.north}} {{loc.east}}
  {{/with}}
{{/with}}

这使得复杂的模板有可能提供比 ../ 深度引用更清晰的代码。

你可以选择提供 {{else}} 部分,该部分仅在传递的值为空时显示。

template
{{#with city}}
{{city.name}} (not shown because there is no city)
{{else}}
No city found
{{/with}}
input
{
  person: {
    firstname: "Yehuda",
    lastname: "Katz",
  },
}

# lookup

lookup 辅助程序允许使用 Handlebars 变量进行动态参数解析。

这对于解析数组索引的值很有用。

template
{{#each people}}
   {{.}} lives in {{lookup ../cities @index}}
{{/each}}

它还可用于根据输入数据查找对象的属性。 以下是一个更复杂的示例,它在子表达式中使用 lookup 根据属性值将求值上下文更改为另一个对象。

template
{{#each persons as | person |}}
    {{name}} lives in {{#with (lookup ../cities [resident-in])~}}
      {{name}} ({{country}})
    {{/with}}
{{/each}}

# log

log 辅助程序允许在执行模板时记录上下文状态。

template
{{log 'this is a simple log output'}}

它委托给 Handlebars.logger.log,可以覆盖该 Handlebars.logger.log 以执行自定义日志记录。

可以将任意数量的参数传递给此方法,并且所有参数都将转发到记录器。

template
{{log 'firstname' firstname 'lastname' lastname}}

可以使用级别散列参数来设置日志级别。 支持的值为 debug、info、warn 和 error。 省略时,info 为默认值,

日志记录是有条件的,基于 Handlebars.logger.level 中设置的级别和值,默认为 info。 所有等于或高于当前级别的日志语句都会被输出。

template
{{log "debug logging" level="debug"}}
{{log "info logging" level="info"}}
{{log "info logging is the default"}}
{{log "logging a warning" level="warn"}}
{{log "logging an error" level="error"}}
Last Updated: 2023/9/14 11:12:03