dongxiji0687
2018-05-22 16:30
浏览 75
已采纳

在Silverstripe中将多个数据阵列传递给模板

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();
$sqlQuery->setFrom('Wine');
$sqlQuery->addWhere($queryArray);
$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'])) {
      $JSBrandsArray->push(array('Brandname'=>$result['BrandName']));
  }
}

Then, the third ArrayList combines these two arrays:

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

$mainArray is passed to the template like so:

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

Then, in the WinesList.ss 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中,我想在单页模板上使用两个循环。 数组是在页面控制器内的单个函数内创建的。

我的想法是创建两个ArrayLists,然后将它们组合成第三个ArrayList,我将其传递给模板。

使用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'])); 
} 
} 
   
 
 

然后,第三个ArrayList组合了这两个数组:

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

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

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

然后,在WinesList.ss模板中,我想我可以这样做:

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

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

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

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

1条回答 默认 最新

相关推荐 更多相似问题