Skip to content

Instantly share code, notes, and snippets.

@andku83
Created October 23, 2017 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andku83/2727fef993e5ebf97c6c1f47fe1926b1 to your computer and use it in GitHub Desktop.
Save andku83/2727fef993e5ebf97c6c1f47fe1926b1 to your computer and use it in GitHub Desktop.
<?php
/**
* @license http://www.yiiframework.com/license/
*/
namespace frontend\widgets;
/**
* The ListView widget is used to display data from data
* provider. Each data model is rendered using the view
* specified.
*
*/
class SiteListPartView extends SiteListView
{
/**
* @var string
*/
public $partLayout = '<div class="row">{part}</div>';
/**
* @var array
*/
public $showBanners = true;
/**
* @var string
*/
public $bannerView = '';
/**
* @var string
*/
public $countInPart = 2;
/**
* @var string
*/
public $defaultItemLayout = '';
/**
* Renders all data models.
* @return string the rendering result
*/
public function renderItems()
{
$models = $this->dataProvider->getModels();
$keys = $this->dataProvider->getKeys();
$partRows = [];
$parts = array_chunk($models,$this->countInPart);
$cnt = 1;
$lastPartIndex = count($parts) - 1;
foreach ($parts as $partKey => $part){
$rows = [];
foreach ($part as $index => $model) {
$key = $keys[$index] + $partKey;
if (($before = $this->renderBeforeItem($model, $key, $index)) !== null) {
$rows[] = $before;
}
$rows[] = $this->renderItem($model, $key, $index);
if (($after = $this->renderAfterItem($model, $key, $index)) !== null) {
$rows[] = $after;
}
}
$cntRows = count($rows);
if($partKey == $lastPartIndex && ($num = $this->countInPart - $cntRows) > 0){
$rows = array_merge($rows, array_fill($cntRows, $num, $this->defaultItemLayout));
}
$partRows[$partKey] = strtr($this->partLayout, [
'{part}' => implode($this->separator, $rows),
]);
// if($this->showBanners && $cnt % $this->countInPart == 0){
if($this->showBanners && $cnt % 4 == 0){
$partRows[$partKey] .= BannerArticleWidget::widget();
}
$cnt++;
}
return implode($this->separator, $partRows);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment