martinways Posted November 23, 2014 Report Share Posted November 23, 2014 Подскажите какое правило надо сделать чтобы каждые 3 товара заворачивались в div, например:<div class=products><li class=product>...</li><li class=product>...</li><li class=product>...</li></div><div class=products><li class=product>...</li><li class=product>...</li><li class=product>...</li></div>и так далее Или другими словами надо после каждого 3-го товара вставить разделитель </div><div class=products> Quote Link to post Share on other sites
Kors Posted November 23, 2014 Report Share Posted November 23, 2014 {foreach $products as $product} {if $product@index%3==0} Начало тройки... {/if} Стандартный вывод {if $product@iteration%3==0 || $product@last } Конец тройки... {/if} {/foreach} Quote Link to post Share on other sites
Noxter Posted November 23, 2014 Report Share Posted November 23, 2014 http://forum.simplacms.ru/topic/7713-верстка-вывод-товаров-по-4шт-в-одном-диве/ Quote Link to post Share on other sites
chocolate_moles Posted November 23, 2014 Report Share Posted November 23, 2014 {foreach $products as $product} {if $product@first || ($product@iteration - 1) is div by 3}<div class=products>{/if} <li class=product>...</li> {if $product@last || $product@iteration is div by 3}</div>{/if} {/foreach} Quote Link to post Share on other sites
martinways Posted November 25, 2014 Author Report Share Posted November 25, 2014 Друзья, спасибо за помощь, сделал по первому варианту Корса и все получилось как и хотел. Quote Link to post Share on other sites
Flexcore Posted December 2, 2019 Report Share Posted December 2, 2019 {foreach $products as $product} {if $product@first || ($product@iteration - 1) is div by 3}<div class=products>{/if} <li class=product>...</li> {if $product@last || $product@iteration is div by 3}</div>{/if} {/foreach} Спасибо, пригодилось! Quote Link to post Share on other sites
DaVinci Posted December 3, 2019 Report Share Posted December 3, 2019 <table> {foreach array_chunk($products, 3) as $chunk} <tr> {foreach $chunk as $product} <td>{$product->name}</td> {/foreach} </tr> {/foreach} </table> Quote Link to post Share on other sites
Flexcore Posted December 3, 2019 Report Share Posted December 3, 2019 <table> {foreach array_chunk($products, 3) as $chunk} <tr> {foreach $chunk as $product} <td>{$product->name}</td> {/foreach} </tr> {/foreach} </table> Любые функции с массивами более ресурсоемкие по сравнению с банальным условием.Но несомненно ваше решение простое и элегантное! Quote Link to post Share on other sites
DaVinci Posted December 4, 2019 Report Share Posted December 4, 2019 (edited) Любые функции с массивами более ресурсоемкие по сравнению с банальным условием.Но несомненно ваше решение простое и элегантное! любые банальные условия более ресурсоемкие по сравнению с их полным отсутствием. если не хотите писать о том что масло масляное то почитайте про оптимизацию, проведите сравнительные тесты. Вы руководствуетесь догадками которые к реальности не имеют отношения. + если считать что затраты на программиста которому приходиться читать тысячу невразумительных условий вместо абстрагированного когда и за это он берет двойную плату за сою работу, то для заказчика это тоже ресурсоемко. пример с условиями в данном случае прост, но когда таких условий становиться много код поддерживать сложно и дорого! писать надо для людей а про оптимизацию думать только в том случае если в этом есть практическая необходимость и если она себя оправдывает. Edited December 4, 2019 by DaVinci Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.