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?

  • 写回答

1条回答 默认 最新

  • dongxi5505 2018-05-24 12:46
    关注

    I realized that I didn't need $mainArray. Multiple arrays can be sent to the template with the renderWith method. Here's the solution I arrived at:

    return $this->renderWith("Layout/WinesList",array(
    'SS_WinesArray' => $SS_WinesArray,
    'JS_CountriesArray' => $JS_CountriesArray,
    'JS_BrandsArray'    => $JS_BrandsArray,
    // more arrays can be added here and looped in the template below
    ));
    
    <% loop $SS_WinesArray %>
    // I do JS stuff with this, which interacts with the categories 
    // I have set up in the other arrays, but prefixed it SS to 
    // differentiate it from the other arrays, which I'm using as category filters
    <% end_loop %>
    
    <% loop $JS_CountriesArray %>
    // do JS stuff with the just the $JS_CountriesArray
    <% end_loop %>
    
    <% loop $JS_BrandsArray %>
    // do JS stuff with the just the $JS_BrandsArray
    <% end_loop %>
    
    // loop through more arrays, if added to the renderWith method above. 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历