在Silverstripe模板中,$ Up标记暂时中断当前循环/并允许访问父作用域.虽然以下模板代码有效,但我不明白为什么.
$ Checked是一个包含我循环的对象的列表,它本身包含一个带有$ Items的列表,我需要显示第一个.该对象还有一个所有者,我需要在第一个项目的范围内访问该所有者.
我有以下silverstripe模板代码:
<% loop $Checked %>
<% with $Items.First %>
<article class="checked-statement">
<span class="statement-outcome h-bg-true-$Verdict.Value">$Verdict.Name</span>
<div class="checked-statement__statement">
<a href="xxx" class="checked-statement__picture"><img
src="$Up.Up.Owner.Photo.CroppedImage(160,160).Url" alt="$Up.Up.Owner.Name.XML" class="h-full-rounded"/></a>
<a href="xxx" class="checked-statement__name">$Up.Up.Owner.Name</a> zei
<h3>
<a href="yyy" class="checked-statement__statement-text">$Up.Up.Title</a>
</h3>
</div>
<a href="yyy" class="checked-statement__argument">
$Up.Up.Statement…
$Top.Icon('chevron-right')
</a>
</article>
<% end_with %>
<% end_loop %>
Run Code Online (Sandbox Code Playgroud)
我想我只需要一个"Up"来达到$ Checked的范围.但我需要两个,这很奇怪,因为第一个Up应该突破"with",第二个应该突破循环,给我顶级范围,其中特定项目不可用.. .
任何人都可以在我的推理或代码中指出我的错误吗?
在某些时候,我相信它是3.0或3.1,$ Up背后的逻辑改变了.
之前<% with $Items.First %>或之后的2.x <% control $Items.First %>只是一个范围级别,您可以突破$ Up.
现在,每个对象/方法调用/属性都获得它自己的范围.这意味着:
<% with $Foo.Bar %>Foo's ID: $Up.ID<% end_with %>
<% with $Foo.Bar %>Outside of "with" ID: $Up.Up.ID (== $Top.ID in this case)<% end_with %>
And for deeper nesting you need even more ups:
<% with $Foo.Bar.X %><% loop $Y %>$Up.Up.Up.Up.ID == $Top.ID<% end_loop %><% end_with %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
106 次 |
| 最近记录: |