该yield()
函数允许您在函数调用范围内编写额外的 Tritium 代码。
例如,您可以wrap()
像这样使用函数:
wrap("div") {
add_class("product")
}
在此示例中,该wrap()
函数将当前节点包围在<div>
标签内,然后将“产品”类添加到该标签中,从而生成以下 HTML:
<div class="product">
<!-- the node you originally selected is now wrapped inside here -->
</div>
函数调用add_class()
是在wrap()
函数yield()
块内执行的。wrap()
函数定义如下所示:
@func XMLNode.wrap(Text %tag) {
%parent_node = this()
insert_at(position("before"), %tag) {
move(%parent_node, this(), position("top"))
yield()
}
}
如您所见,yield()
函数定义中的调用 forwrap()
让 Tritium 代码将其执行交给add_class()
我上面编写的函数。
因此,再次使用我的示例,这部分代码:
wrap("div") {
add_class("product")
}
就像写:
%parent_node = this()
insert_at(position("before"), "div") {
move(%parent_node, this(), position("top"))
add_class("product") ## <-- This is where the code inside a yield() block gets added
}