# 局部
¥Partials
Handlebars 允许通过部分模板重用。Partials 是普通的 Handlebars 模板,可以被其他模板直接调用。
¥Handlebars allows for template reuse through partials. Partials are normal Handlebars templates that may be called directly by other templates.
# 基本局部
¥Basic Partials
为了使用部分,必须通过 Handlebars.registerPartial
注册。
¥In order to use a partial, it must be registered via Handlebars.registerPartial
.
此调用将注册 myPartial
部分。部分可以被预编译,并将预编译模板传递到第二个参数中。
¥This call will register the myPartial
partial. Partials may be precompiled and the precompiled template passed into
the second parameter.
调用部分是通过部分调用语法完成的:
¥Calling the partial is done through the partial call syntax:
将渲染名为 myPartial
的部分。当部分执行时,它将在当前执行上下文下运行。
¥Will render the partial named myPartial
. When the partial executes, it will be run under the current execution
context.
# 动态局部
¥Dynamic Partials
可以使用子表达式语法动态选择要执行的部分。
¥It's possible to dynamically select the partial to be executed by using sub expression syntax.
将评估 whichPartial
,然后渲染该函数返回名称的部分。
¥Will evaluate whichPartial
and then render the partial whose name is returned by this function.
子表达式不解析变量,因此 whichPartial
必须是一个函数。如果一个简单变量具有部分名称,则可以通过 lookup
辅助程序来解析它。
¥Subexpressions do not resolve variables so whichPartial
must be a function. If a simple variable has the partial name,
it's possible to resolve it via the lookup
helper.
# 局部上下文
¥Partial Contexts
通过将上下文传递给部分调用,可以在自定义上下文上执行部分。
¥It's possible to execute partials on a custom context by passing in the context to the partial call.
# 局部参数
¥Partial Parameters
自定义数据可以通过哈希参数传递给部分。
¥Custom data can be passed to partials through hash parameters.
部分运行时将参数设置为 value
。
¥Will set parameter to value
when the partial runs.
这对于将父上下文中的数据公开给部分上下文特别有用:
¥This is particularly useful for exposing data from parent contexts to the partial:
# 局部块
¥Partial Blocks
尝试渲染未找到的部分时的正常行为是实现抛出错误。如果需要故障转移,则可以使用块语法调用部分。
¥The normal behavior when attempting to render a partial that is not found is for the implementation to throw an error. If failover is desired instead, partials may be called using the block syntax.
如果 myPartial
部分未注册,则将渲染 Failover content
。
¥Which will render Failover content
if the myPartial
partial is not registered.
此块语法还可用于将模板传递给部分,该部分可以由专门命名的部分 @partial-block
执行。一个模板
¥This block syntax may also be used to pass templates to the partial, which can be executed by the specially named
partial, @partial-block
. A template of
layout
部分包含
¥with the layout
partial containing
会渲染
¥Would render
当以这种方式调用时,该块将在调用时的部分上下文中执行。深度路径和块参数相对于部分块而不是部分模板进行操作。
¥When called in this manner, the block will execute under the context of the partial at the time of the call. Depthed paths and block parameters operate relative to the partial block rather than the partial template.
将从该模板渲染 person.firstname
,而不是部分渲染。
¥Will render person.firstname
from this template, not the partial.
# 内嵌局部
¥Inline Partials
模板可以通过 inline
装饰器定义块作用域的部分。
¥Templates may define block scoped partials via the inline
decorator.
这将为每个子级渲染 myPartial
部分。
¥Which will render the myPartial
partial for each child.
每个内联部分可用于当前块和所有子块,包括其他部分的执行。这允许布局模板和类似的功能:
¥Each inline partial is available to the current block and all children, including execution of other partials. This allows for layout templates and similar functionality:
其中 layout
部分可能是:
¥Where the layout
partial may be: