【问题标题】:Silverstripe: Template behavior of "Up"Silverstripe:“向上”的模板行为
【发布时间】:2017-01-04 20:03:40
【问题描述】:

在 Silverstripe 模板中,$Up 标签会暂时跳出当前循环/with 并提供对父作用域的访问权限。虽然以下模板代码有效,但我不明白为什么。

$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&hellip;
                $Top.Icon('chevron-right')
            </a>
        </article>
    <% end_with %>
<% end_loop %>

我想我只需要一个“Up”就可以进入 $Checked 的范围。但我需要两个,这很奇怪,因为第一个 Up 应该跳出“with”,第二个应该跳出循环,给我顶级范围,其中特定项目不可用.. .

谁能指出我的推理或代码中的错误?

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    在某些时候,我相信是 3.0 或 3.1,$Up 背后的逻辑发生了变化。

    以前的 &lt;% with $Items.First %&gt; 或回到 2.x 的 &lt;% control $Items.First %&gt; 只是您可以使用 $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 %>
    

    【讨论】:

    • 啊,所以第一个 Up 我只突破了第一个,并最终出现在 Item 列表中。然后第二个突破了 $Items。这就说得通了。谢谢!
    猜你喜欢
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2016-07-22
    • 1970-01-01
    相关资源
    最近更新 更多