$items = $modx->fromJSON($input);

krsort($items);
$items = array_values($items);

if (!count($items) || empty($tpl)) return '';

$modx->setPlaceholder('total', count($items));

$limit = isset($limit) ? (integer)$limit : 5;
$offset = isset($offset) ? (integer)$offset : 0;

$items = $offset > 0 ? array_slice($items, $offset) : $items;
$count = count($items);

$limit = $limit == 0 || $limit > $count ? $count : $limit;

$output = $tempitems = array();

for ($i = 0; $i < $limit; $i++) {
	if ($i >= $count) {
		break;
	}
	$tempitems[] = $items[$i];
}
$items = $tempitems;

foreach ($tempitems as $i=>$row) {
	$output[] = $modx->getChunk($tpl, $row);
}

return implode("\n", $output);