# 实用函数

¥Utility functions

# 辅助实用程序

¥Helper utilities

当你实现自定义助手时,这些方法会很方便。

¥These methods come handy when you implement custom helpers.

# Handlebars.SafeString(string)

防止渲染模板时 string 被转义。

¥Prevents string from being escaped when the template is rendered.

new Handlebars.SafeString("<div>HTML Content!</div>");

在构造将标记为安全的字符串时,应使用 Handlebars.escapeExpression 方法正确转义任何外部内容,以避免潜在的安全问题。

¥When constructing the string that will be marked as safe, any external content should be properly escaped using the Handlebars.escapeExpression method to avoid potential security concerns.

# Handlebars.escapeExpression(string)

HTML 对传递的字符串进行转义,使其可以安全地渲染为 HTML 内容中的文本。

¥HTML escapes the passed string, making it safe for rendering as text within HTML content.

Handlebars.Utils.escapeExpression(string);

&<>"'、```、= 替换为字符串值的 HTML 实体等效值。SafeString 值保持不变。

¥Replaces &, <, >, ", ', `, = with the HTML entity equivalent value for string values. SafeString values are left untouched.

除了三大括号表达式之外的所有表达式的输出都通过此方法传递。当通过 SafeString 实例返回 HTML 内容时,助手也应该使用此方法,以防止可能的代码注入。

¥The output of all expressions except for triple-braced expressions are passed through this method. Helpers should also use this method when returning HTML content via a SafeString instance, to prevent possible code injection.

该方法的别名为 Handlebars.Utils.escapeExpression

¥This method is aliased at Handlebars.Utils.escapeExpression.

# Handlebars.createFrame(data)

由块助手用来创建子数据对象。

¥Used by block helpers to create child data objects.

if (options.data) {
  var data = Handlebars.createFrame(options.data);
  data.foo = "bar";
  options.data = data;
}

修改数据状态的助手应该在这样做时创建一个新框架,以隔离自己并避免破坏任何父级的状态。通常,每次助手执行只需要创建一帧。例如,each 迭代器创建一个单帧,该帧可重用于所有子执行。

¥Helpers that modify the data state should create a new frame when doing so, to isolate themselves and avoid corrupting the state of any parents. Generally, only one frame needs to be created per helper execution. For example, the each iterator creates a single frame which is reused for all child execution.

# 通用实用工具

¥General Utilities

Handlebars 提供了通过 Handlebars.Utils 对象公开的各种实用方法。

¥Handlebars offers a variety of utility methods that are exposed through the Handlebars.Utils object.

# Handlebars.Utils.isEmpty(value)

确定给定值是否为空。

¥Determines if a given value is empty.

Handlebars.Utils.isEmpty(value)

内置 ifwith 辅助程序使用它来控制它们的执行流程。Handlebars 对空的定义是:

¥This is used by the built-in if and with helpers to control their execution flow. The Handlebars definition of empty is any of:

  • 长度为 0 的数组

    ¥Array with length 0

  • 0 以外的假值

    ¥falsy values other than 0

这是为了匹配 Mustache 行为 (opens new window)

¥This is intended to match the Mustache behavior (opens new window).

# Handlebars.Utils.extend(obj, value)

使用 value 上定义的所有键来扩充 obj 的简单实用方法。

¥Simple utility method to augment obj with all keys defined on value.

Handlebars.Utils.extend(foo, {bar: true})

将使用值 true 设置对象 foo 上的键 bar

¥Will set the key bar on object foo with the value true.

# Handlebars.Utils.toString(obj)

通用 toString 方法。

¥Generic toString method.

# Handlebars.Utils.isArray(obj)

确定对象是否为数组。

¥Determines if an object is an array.

# Handlebars.Utils.isFunction(obj)

确定对象是否是函数。

¥Determines if an object is a function.

# Handlebars.log(level, message)

log 辅助程序使用的记录器。

¥Logger used by the log helper.

如果需要的话可以被覆盖。

¥May be overridden if desired.

Last Updated: 2024/3/9 17:51:41