2018-05-22 16:30
浏览 75


In Silverstripe 4, I'd like to use two Loops on a single page template. The arrays are created inside of a single function inside my Page Controller.

My idea was to create two ArrayLists, then combine them into a third ArrayList, which I pass to the template.

Using SQLSelect, I have some code that creates an ArrayList of data. $queryArray is an array of key=>value pairs.

$sqlQuery = new SQLSelect();
$results = $sqlQuery->execute();
$SSArrayList = new ArrayList; //new ArrayList;

foreach($results as $result) {
    $SSArrayList->push(new ArrayData($result));

I have some other code that creates another ArrayList from the same $results:

foreach($results as $result) {
  if (!empty($result['BrandName'])) {

Then, the third ArrayList combines these two arrays:

$mainArray = new ArrayList;

$mainArray is passed to the template like so:

return $this->customise(array('MainArray'=>$mainArray))->renderWith("Layout/WinesList");

Then, in the template, I thought I could do something like this:

<% loop $MainArray %>
    <% loop $SSArrayList %>
    // show results from $SSArrayList
    <% end_loop %>

    <% loop $JSBrandsArray %>
    // show results from $JSBrandsArray
    <% end_loop %>
<% end_loop %>

If I var_dump() $mainArray from the page controller, $mainArray seems to have all the data, but I can't figure out how to properly access the data from the template.

Is this even possible? If so, what am I doing wrong?

图片转代码服务由CSDN问答提供 功能建议

在Silverstripe 4中,我想在单页模板上使用两个循环。 数组是在页面控制器内的单个函数内创建的。


使用SQLSelect,我有一些代码可以创建数据的ArrayList。 $ queryArray是key =&gt;值对的数组。

  $ sqlQuery = new SQLSelect(); 
 $ sqlQuery-&gt; setFrom('Wine'); 
 $ sqlQuery-&gt; addWhere($ queryArray); \  n $ results = $ sqlQuery-&gt; execute(); 
 $ SSArrayList = new ArrayList;  // new ArrayList; 
foreach($ results as $ result){
 $ SSArrayList-&gt; push(new ArrayData($ result)); 
 \ 我有一些其他代码从同一个$ results创建另一个ArrayList: 
  foreach($ results as $ result){
 if(!empty($)  result ['BrandName'])){
 $ JSBrandsArray-&gt; push(array('Brandname'=&gt; $ result ['BrandName'])); 


  $ mainArray = new ArrayList; 
 $ mainArray-&gt; push($ SSArrayList  ); 
 $ mainArray-&gt; push($ JSBrandsArray); 

$ mainArray传递给模板,如下所示:

 返回$ this-&gt; customize(array('MainArray'=&gt; $ mainArray)) - &gt; renderWith(“Layout / WinesList”); 


 &lt;%loop $ MainArray%&gt; 
&lt;  %loop $ SSArrayList%&gt; 
 //显示来自$ SSArrayList 
&lt;%end_loop%&gt的结果 ; 
&lt;%loop $ JSBrandsArray%&gt; 
 //显示来自$ JSBrandsArray 

如果我从页面控制器获取var_dump()$ mainArray,$ mainArray似乎拥有所有数据,但我无法弄清楚如何正确访问模板中的数据。

这甚至可能吗? 如果是这样,我做错了什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题