public function indexAction()
{
$products = new ProductsCollection;
$vm = new ProductsViewModel($products);
return $vm;
}
class ProductsViewModel ViewModel extends
{
public function __construct(ProductsCollection $products)
{
$items = array();
foreach ($products as $product) {
$items[] = [
'url' => '/product/' . $product['id'],
'price' => !empty($product['price']) ? \number_format($product['price'], 2, ',', ' ') : ",
'title' => $product['title'],
'class' => empty($product['hot price']) ? ": 'active',
'showCartBtn' => !empty($product['price'])
];
}
$this->setVariable('items', $items);
}
}
the <ul>
<?php foreach ($items as $item): ?>
the <li>
the <div>
<a href="<?= $this->escapeHtml($item['url']) ?>" class="<?= $item['class'] ?>">
<?= $item['title'] ?>
</a>
</div>
the <div>
<?php if ($item['showCartBtn']):?>
<form method="post">
<button type="submit" class="btn btn-primary">
Buy now for <?= $item['price'] ?>
</button>
</form>
<?php else:?>
<button type="submit" class="btn">
Sold out
</button>
<?php endif;?>
</div>
</li>
<?php endforeach; ?>
</ul>
return (new ViewModel())
->setVariables($productsView->getItemsVariables())
;
Find more questions by tags Zend Framework