Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Silverstripe模板中从循环中断_Php_Templates_Silverstripe - Fatal编程技术网

Php 在Silverstripe模板中从循环中断

Php 在Silverstripe模板中从循环中断,php,templates,silverstripe,Php,Templates,Silverstripe,简单地说,在模板文件(.ss)中,是否有可能从循环中断开 例如 您有一个ArrayList,在找到所需的键之前,您希望对其进行循环。在这个场景中,我需要跟踪MyRequestVar,然后立即退出循环 <% loop $RequestVars %> <% if $Key == "MyRequestVar" %> MyRequestVar is set to $Value <% break_loop %> <%

简单地说,在模板文件(.ss)中,
是否有可能从循环中断开

例如

您有一个ArrayList,在找到所需的键之前,您希望对其进行循环。在这个场景中,我需要跟踪
MyRequestVar
,然后立即退出循环

<% loop $RequestVars %>
    <% if $Key == "MyRequestVar" %>
        MyRequestVar is set to $Value
        <% break_loop %>
    <% end_if %>
<% end_loop %>

MyRequestVar设置为$Value

当然,我可以让它继续迭代ArrayList,它将按预期工作,但我的OCD正在启动perf++

在您提供的上下文中不可能中断循环

但是,您可以从模板中筛选ArrayList,如下所示:

<% loop $RequestVars.filter(Key, "MyRequestVar") %>


您还可以使用

在您提供的上下文中,不可能中断循环

但是,您可以从模板中筛选ArrayList,如下所示:

<% loop $RequestVars.filter(Key, "MyRequestVar") %>


您也可以使用

在PHP代码中这样做会更好。我不同意您的观点:)如果您需要一个修饰符,例如:greaterthanfilter不打破循环,但比它更好;我不同意你的观点:)如果你需要一个修饰符,比如:greaterthanfilter不打破循环,但比它更好;把它作为@munomono的一个答案放在一边:因为可以在模板和$list(“Page”)中使用filter/sort/limit来获取数据对象,所以很少需要编写php getter。也许有人会说这是一种不好的做法,但我喜欢:)好吧,
$LatestNews
$List('News')更具声明性。排序(…)。限制(5)
,但这主要是一个偏好问题。请注意:因为您可以在模板和$List(“页面”)中使用filter/sort/limit来获取数据对象,所以很少需要编写php getter。也许有人会说这是一种糟糕的做法,但我喜欢:)嗯,
$LatestNews
$List('News')更具声明性。排序(…)。限制(5)
,但这主要是偏好的问题