我正在查看Tritium API网站上的示例,但我不理解what ()函数的作用。
http://tritium.io/simple-mobile/1.0.224#yield()%20Text
有人能解释一下这些例子吗?
# first example
@func XMLNode.foo {
$a = "dog"
yield()
\ log($a)
}
# second example
foo() {
$a = $a + "cat"
}
@func XMLNode.foo {
$a = "dog"
log($a)
yield()
}
foo() {
$a = $a + "cat"
}发布于 2013-05-30 17:46:20
yield()函数允许您在函数调用的范围内编写额外的TRI3代码。
例如,您可以像这样使用wrap()函数:
wrap("div") {
add_class("product")
}在此示例中,wrap()函数将当前节点置于<div>标记内,然后向该标记添加"product“类,结果如下:
<div class="product">
<!-- the node you originally selected is now wrapped inside here -->
</div>对add_class()的函数调用在wrap()函数的yield()块中执行。wrap() function definition如下所示:
@func XMLNode.wrap(Text %tag) {
%parent_node = this()
insert_at(position("before"), %tag) {
move(%parent_node, this(), position("top"))
yield()
}
}正如您所看到的,wrap()函数定义中的yield()调用允许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
}https://stackoverflow.com/questions/16830324
复制相似问题