【问题标题】:Passing Multiple Arrays of Data to Template in Silverstripe将多个数据数组传递给 Silverstripe 中的模板
【发布时间】:2018-11-01 12:12:27
【问题描述】:

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

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

使用 SQLSelect,我有一些代码可以创建数据的 ArrayList。 $queryArray 是一个 key=>value 对的数组。

$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));
}

我还有一些其他代码可以从相同的 $results 创建另一个 ArrayList:

foreach($results as $result) {
  if (!empty($result['BrandName'])) {
      $JSBrandsArray->push(array('Brandname'=>$result['BrandName']));
  }
}

然后,第三个 ArrayList 将这两个数组组合起来:

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

$mainArray 像这样传递给模板:

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

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

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

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

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

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

【问题讨论】:

  • 您认为您将键命名为 SSArrayList 和 JSBrandsArray 以便迭代器在每个循环中为它们提供它们。此外,如果您将两个项目添加到列表或数组中,除了每个“循环”它们都可以读取或每次迭代只能读取一个之外,您会做什么?
  • 关注docs.silverstripe.org/en/4/developer_guides/templates/… 并执行类似 $arrayData = new SilverStripe\View\ArrayData(['alist' => $SSArrayListofsomething ,'otherlist' => $SSArrayListofsomethingElse]);
  • 这看起来很像你应该为这两个列表中的每一个使用单独的控制器方法而不是通过组合它们来使事情复杂化
  • 你总是可以使用$TopLevelArray.First和$TopLevelArray.Last
  • 为什么不按照@Olli 的建议明确命名您的密钥。尽管 Robbie's 可能会起作用,但我也一样,这将是一个 hack,并且可能会在以后弄乱您的模板,因为这确实为您提供了扩展的空间。 $mainArray-&gt;push(ArrayData::create([ 'SSArrayList'=&gt;$SSArrayList ])); $mainArray-&gt;push(ArrayData::create([ 'JSBrandsArray'=&gt;$JSBrandsArray ])); 这样你就不会改变你的模板了。

标签: php arrays silverstripe silverstripe-4


【解决方案1】:

我意识到我不需要 $mainArray。可以使用 renderWith 方法将多个数组发送到模板。这是我得出的解决方案:

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. 

【讨论】:

    猜你喜欢
    • 2016-05-15
    • 1970-01-01
    • 2017-09-23
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多