# (预)汇编

¥(Pre-)Compilation

# Handlebars.compile(template, options)

编译模板以便可以立即执行。

¥Compiles a template so it can be executed immediately.

const template = Handlebars.compile("{{foo}}");
template({});

支持多种改变模板执行方式的选项。

¥Supports a variety of options that alter how the template executes.

  • data:设置为 false 以禁用 @data 跟踪。

    ¥data: Set to false to disable @data tracking.

  • compat:设置为 true 以启用递归字段查找。

    ¥compat: Set to true to enable recursive field lookup.

  • knownHelpers:包含在模板执行时已知存在(真实)的辅助程序列表的哈希值。通过这个允许编译器优化许多情况。内置助手会自动包含在此列表中,并且可以通过将该值设置为 false 来省略。

    ¥knownHelpers: Hash containing list of helpers that are known to exist (truthy) at template execution time. Passing this allows the compiler to optimize a number of cases. Builtin helpers are automatically included in this list and may be omitted by setting that value to false.

  • knownHelpersOnly:设置为 true 以允许基于已知辅助程序列表进行进一步优化。

    ¥knownHelpersOnly: Set to true to allow further optimizations based on the known helpers list.

  • noEscape:设置为 true 不会 HTML 转义任何内容。

    ¥noEscape: Set to true to not HTML escape any content.

  • strict:以严格模式运行。在这种模式下,模板将抛出而不是默默地忽略丢失的字段。除非字段明确包含在源对象中,否则这会产生禁用反向操作(例如 {{^foo}}{{/foo}})的副作用。

    ¥strict: Run in strict mode. In this mode, templates will throw rather than silently ignore missing fields. This has the side effect of disabling inverse operations such as {{^foo}}{{/foo}} unless fields are explicitly included in the source object.

  • assumeObjects:遍历路径时删除对象存在检查。这是 strict 模式的子集,当已知数据输入安全时,该模式会生成优化模板。

    ¥assumeObjects: Removes object existence checks when traversing paths. This is a subset of strict mode that generates optimized templates when the data inputs are known to be safe.

  • preventIndent:默认情况下,缩进的部分调用会导致整个部分的输出缩进相同的量。当部分写入 pre 标签时,这可能会导致意外行为。将此选项设置为 true 将禁用自动缩进功能。

    ¥preventIndent: By default, an indented partial-call causes the output of the whole partial being indented by the same amount. This can lead to unexpected behavior when the partial writes pre-tags. Setting this option to true will disable the auto-indent feature.

  • ignoreStandalone:设置为 true 时禁用独立标签删除。设置后,位于自己行上的块和部分不会删除该行上的空格。

    ¥ignoreStandalone: Disables standalone tag removal when set to true. When set, blocks and partials that are on their own line will not remove the whitespace on that line.

  • explicitPartialContext:禁用部分的隐式上下文。启用后,未传递上下文值的部分将针对空对象执行。

    ¥explicitPartialContext: Disables implicit context for partials. When enabled, partials that are not passed a context value will execute against an empty object.

# Handlebars.precompile(template, options)

预编译给定模板,以便可以将其发送到客户端并无需编译即可执行。

¥Precompiles a given template so it can be sent to the client and executed without compilation.

var templateSpec = Handlebars.precompile("{{foo}}");

支持与 Handlebars.compile 方法相同的所有选项参数。另外可能会通过:

¥Supports all of the same options parameters as the Handlebars.compile method. Additionally may pass:

  • srcName:传递以生成输入文件的源映射。当以这种方式运行时,返回结构是 {code, map},其中 code 包含模板定义,map 包含源映射。

    ¥srcName: Passed to generate the source map for the input file. When run in this manner, the return structure is {code, map} with code containing the template definition and map containing the source map.

  • destName:可选参数与 srcName 结合使用,在生成源映射时提供目标文件名。

    ¥destName: Optional parameter used in conjunction with srcName to provide a destination file name when generating source maps.

# Handlebars.template(templateSpec)

设置使用 Handlebars.precompile 预编译的模板。

¥Sets up a template that was precompiled with Handlebars.precompile.

var template = Handlebars.template(templateSpec);
template({});
Last Updated: 2024/3/9 17:51:41