# Handlebars 运行时

¥The Handlebars runtime

# Handlebars.registerPartial(name, partial)

注册环境中任何模板均可访问的部分。

¥Registers partials accessible by any template in the environment.

Handlebars.registerPartial("foo", partial);

还支持一次注册多个部分。

¥Also supports registering multiple partials at once.

Handlebars.registerPartial({ foo: partial, bar: partial });

如果加载整个库,部分内容可能是字符串值,将根据需要进行编译。如果仅加载运行时,则部分必须是已使用 Handlebars.template 方法正确设置的预编译模板。

¥If loading the whole library, the partials may be string values which will be compiled on demand. If only loading the runtime, the partials must be a precompiled template that has been set up properly using the Handlebars.template method.

# Handlebars.unregisterPartial(name)

取消注册先前注册的部分。

¥Unregisters a previously registered partial.

Handlebars.unregisterPartial("foo");

# Handlebars.registerHelper(name, helper)

注册可通过环境中的任何模板访问的辅助程序。

¥Registers helpers accessible by any template in the environment.

Handlebars.registerHelper("foo", function() {});

还支持一次注册多个助手。

¥Also supports registering multiple helpers at once.

Handlebars.registerHelper({ foo: function() {}, bar: function() {} });

# Handlebars.unregisterHelper(name)

取消注册先前注册的助手。

¥Unregisters a previously registered helper.

Handlebars.unregisterHelper("foo");

# Handlebars.registerDecorator(name, helper)(已弃用)

¥Handlebars.registerDecorator(name, helper) (deprecated)

弃用通知

自定义装饰器已被弃用,并且可能会在 Handlebars 的下一个主要版本中消失。它们暴露了太多的内部 API,很难移植到其他语言,并且使代码更难以维护。

¥Custom decorators are deprecated and may vanish in the next major version of Handlebars. They expose a too large part of the internal API which is difficult to port to other languages and makes to code harder to maintain.

注册一个可由环境中任何模板访问的装饰器。

¥Registers a decorator accessible by any template in the environment.

Handlebars.registerDecorator("foo", function() {});

还支持一次注册多个装饰器。

¥Also supports registering multiple decorators at once.

Handlebars.registerDecorator({ foo: function() {}, bar: function() {} });

# Handlebars.unregisterDecorator(name)

取消注册先前注册的装饰器。

¥Unregisters a previously registered decorator.

Handlebars.unregisterDecorator("foo");

# Handlebars.create()

创建一个隔离的 Handlebars 环境。

¥Creates an isolated Handlebars environment.

var OtherHandlebars = Handlebars.create();

每个环境都有自己的助手和部分。这仅对于需要不同助手或部分的用例是必要的。大多数用例可以直接使用根 Handlebars 环境。

¥Each environment has its own helpers and partials. This is only necessary for use cases that demand distinct helpers or partials. Most use cases can use the root Handlebars environment directly.

为给定环境创建的模板绑定到该环境。这意味着需要在多个环境中运行的模板将需要通过 Handlebars.template 为每个环境重新编译或重建。这也适用于部分。

¥Templates created for a given environment are bound to that environment. This means that templates that need to run in multiple environments will need to be recompiled or reconstructed via Handlebars.template for each environment. This applies to partials as well.

# Handlebars.noConflict()

从全局命名空间中删除此 Handlebars 实例,将全局 Handlebars 变量恢复为其之前的值。

¥Removes this Handlebars instance from the global namespace, restoring the global Handlebars variable to its previous value.

var myHandlebars = Handlebars.noConflict();

这允许同时使用库的不同版本,而不用担心版本冲突。

¥This allows for distinct versions of the library to be used simultaneously without concern for version conflicts.

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